由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Database版 - Interesting Data Manipulation question
相关主题
问一个关于SQL的问题SQL server 2000有hidden records吗?
[Mysql] how to return NULL count in group by query (转载)请问T-SQL中Group By之后怎么找到特定的record
再现急求答案,多谢。SQL SERVER 面试题, find the objects
有趣的Join问题,源于SQL和SAS比较。菜鸟问.asp 里的select语句在基于SQL sever和Access语法上的不
T-SQL Update Statement Question问个SQL问题
大家帮我看看这个function 哪里出了问题。 谢谢SQL中怎样用定制列排序?
help about SQL for ACCESS服了,这就是我们QA和DBA的水平
ask for help with a simple query!!!问个SQL问题- partial outer join
相关话题的讨论汇总
话题: card话题: select话题: test1话题: a234话题: sql
进入Database版参与讨论
1 (共1页)
n********6
发帖数: 1511
1
The original question is for SAS developers. However, I think the problem
can be solved by using SQL in database. I am working on that now for fun.
h******l
发帖数: 422
2
first of all you will need an unique ID for each row added to make this
possible. For example:
Table: TEST1
id cn charge
j*****n
发帖数: 1781
3
It is a good solution.
btw, try CTE with ROW_NUMBER() in 2k5 and 2k8. Same way to solve problem but
looks fancier.

【在 h******l 的大作中提到】
: first of all you will need an unique ID for each row added to make this
: possible. For example:
: Table: TEST1
: id cn charge

B*****g
发帖数: 34098
4
1. 显然要pk
2. 显然要timestamp,要是先负后正的不能删,比如
Card_Number Charge Date
A123 -14.56 2008/01/01
A123 15.23 2008/01/02
A123 14.56 2008/01/03
那个A123都不能取消
3. 如果负的不多应该用loop一个一个找
4. 不过大家还是喜欢拽sql,咱也写一个,没测试,oracle。
B*****g
发帖数: 34098
5
这套数据上面的sql work吗?
Card_Number Charge
A123 15.23
A123 -14.56
A234 11.12
A234 3.87
A234 11.12
A234 -11.12
A234 4.86

but

【在 j*****n 的大作中提到】
: It is a good solution.
: btw, try CTE with ROW_NUMBER() in 2k5 and 2k8. Same way to solve problem but
: looks fancier.

j*****n
发帖数: 1781
6
Yes, it works. I also tried adding more cases and seems fine.
btw, his code is added into my knowledge base, so I wouldn't have headache
in the future... just like your RANK()... hehe...

【在 B*****g 的大作中提到】
: 这套数据上面的sql work吗?
: Card_Number Charge
: A123 15.23
: A123 -14.56
: A234 11.12
: A234 3.87
: A234 11.12
: A234 -11.12
: A234 4.86
:

B*****g
发帖数: 34098
7
我看错了。你为啥不存我的?哈哈

【在 j*****n 的大作中提到】
: Yes, it works. I also tried adding more cases and seems fine.
: btw, his code is added into my knowledge base, so I wouldn't have headache
: in the future... just like your RANK()... hehe...

n********6
发帖数: 1511
8
I worked out a SAS version where SQL is extensively used and can be easily
converted into database. Next step is to see the difference in SAS solution
and database solution.
/* LOAD IN DATA */
DATA CARD;
INPUT Card_Number $ Charge;
CARDS;
A123 14.56
A123 15.23
A123 -14.56
A234 11.12
A234 3.87
A234 11.12
A234 -11.12
A234 4.86
;
RUN;
/* CREATE TEMP TABLE FOR DISTINCT CARD_NUMBER, ADD NEW COLUMN AS ID */
PROC SQL;
CREATE TABLE CARD_NUM_STAGE AS
SELECT Card_Number, Count(Card_Number) AS Card_PerNum_
n********6
发帖数: 1511
9
厉害啊。我一开始就想用join来做,因为join速度最快。可是没搞出来。

【在 h******l 的大作中提到】
: first of all you will need an unique ID for each row added to make this
: possible. For example:
: Table: TEST1
: id cn charge

n********6
发帖数: 1511
10
能解释一下吗?
1,2:z的where clause写在select clause里面,起什么作用呢?
3:t alias 派什么作用?
select * from @TEST1 y
where id not in
(select id
from (select
(select top 1 a.id from TEST1
left outer join @TEST1 b on a.cn = b.cn
and a.charge = (b.charge * -1)
and a.charge > 0
where z.cn = a.cn /*1*/
and a.charge = -z.charge /*2*/
) as id
from @TEST1 z
)t /*3*/
where id i

【在 h******l 的大作中提到】
: first of all you will need an unique ID for each row added to make this
: possible. For example:
: Table: TEST1
: id cn charge

相关主题
大家帮我看看这个function 哪里出了问题。 谢谢SQL server 2000有hidden records吗?
help about SQL for ACCESS请问T-SQL中Group By之后怎么找到特定的record
ask for help with a simple query!!!SQL SERVER 面试题, find the objects
进入Database版参与讨论
n********6
发帖数: 1511
11
SOLUTION 2
PROC SQL;
CREATE TABLE CARD_OUTPUT AS
SELECT * FROM (SELECT * FROM CARD A WHERE A.CHARGE>0)
EXCEPT ALL (SELECT CARD_NUMBER, ABS(CHARGE) FROM CARD B WHERE B.CHARGE < 0);
QUIT;
h******l
发帖数: 422
12
z在这里起到partition by的作用, 如果你用 RANK()的话.
t alias的作用是给temp table的.

【在 n********6 的大作中提到】
: 能解释一下吗?
: 1,2:z的where clause写在select clause里面,起什么作用呢?
: 3:t alias 派什么作用?
: select * from @TEST1 y
: where id not in
: (select id
: from (select
: (select top 1 a.id from TEST1
: left outer join @TEST1 b on a.cn = b.cn
: and a.charge = (b.charge * -1)

h******l
发帖数: 422
13
SQL SERVER没有except all :(
这样很简单.

);

【在 n********6 的大作中提到】
: SOLUTION 2
: PROC SQL;
: CREATE TABLE CARD_OUTPUT AS
: SELECT * FROM (SELECT * FROM CARD A WHERE A.CHARGE>0)
: EXCEPT ALL (SELECT CARD_NUMBER, ABS(CHARGE) FROM CARD B WHERE B.CHARGE < 0);
: QUIT;

n********6
发帖数: 1511
14
sql server and oracle都有实现类似功能的语句,似乎是except, minus, 或其他。

【在 h******l 的大作中提到】
: SQL SERVER没有except all :(
: 这样很简单.
:
: );

h******l
发帖数: 422
15
有EXCEPT但是没有EXCEPT ALL. 这样会受到很多的限制.

【在 n********6 的大作中提到】
: sql server and oracle都有实现类似功能的语句,似乎是except, minus, 或其他。
n********6
发帖数: 1511
16
刚查sql server 2005 books online,
有except/intersect
没有all option

【在 h******l 的大作中提到】
: 有EXCEPT但是没有EXCEPT ALL. 这样会受到很多的限制.
1 (共1页)
进入Database版参与讨论
相关主题
问个SQL问题- partial outer joinT-SQL Update Statement Question
SQL find distinct values in large table大家帮我看看这个function 哪里出了问题。 谢谢
SQL run a stored procedure by fetching from a cursor row by rowhelp about SQL for ACCESS
SQL select one value column for each distinct value another (转载)ask for help with a simple query!!!
问一个关于SQL的问题SQL server 2000有hidden records吗?
[Mysql] how to return NULL count in group by query (转载)请问T-SQL中Group By之后怎么找到特定的record
再现急求答案,多谢。SQL SERVER 面试题, find the objects
有趣的Join问题,源于SQL和SAS比较。菜鸟问.asp 里的select语句在基于SQL sever和Access语法上的不
相关话题的讨论汇总
话题: card话题: select话题: test1话题: a234话题: sql