m**********2 发帖数: 2252 | 1 请教一下这个query应该怎样写哈
有call history table如下,一个ID可能被call多次:
ID callresult calldate
1 null null
1 R sep 2
2 S sep 1
3 null null
3 null null
……
我想找出callresult从来都是null的ID,曾经有过null但是还是有别的result的不包括
在内。
这个应该怎样写?
谢谢啦! |
e*u 发帖数: 17 | 2 select * from tbl
where id not in
(select id from tbl where callresult is not null)
【在 m**********2 的大作中提到】 : 请教一下这个query应该怎样写哈 : 有call history table如下,一个ID可能被call多次: : ID callresult calldate : 1 null null : 1 R sep 2 : 2 S sep 1 : 3 null null : 3 null null : …… : 我想找出callresult从来都是null的ID,曾经有过null但是还是有别的result的不包括
|
d*******8 发帖数: 3182 | 3 select ID from table where ID not in (select ID from table where callresult
not null)? |
d*******8 发帖数: 3182 | 4 en,打字打了5分钟
callresult
【在 d*******8 的大作中提到】 : select ID from table where ID not in (select ID from table where callresult : not null)?
|
m**********2 发帖数: 2252 | 5 en .... 刚才突然短路。。。
OK. Got!
callresult
【在 d*******8 的大作中提到】 : select ID from table where ID not in (select ID from table where callresult : not null)?
|
w*r 发帖数: 2421 | 6 ft...
我回答一下好了,你们写东西从来不动脑子,不是写出来就完了,要高效
SELECT ID,
SUM(CASE WHEN CALLRESULT IS NULL THEN 0 ELSE 1 END) AS CALLFLAG
FROM HISTORYTABLE
GROUP BY ID
HAVING CALLFALG = 0
【在 m**********2 的大作中提到】 : en .... 刚才突然短路。。。 : OK. Got! : : callresult
|
c*******e 发帖数: 8624 | 7 而且not in是一种比较危险的写法,万一那个
subquery里面id有null的话,结果就不对了
【在 w*r 的大作中提到】 : ft... : 我回答一下好了,你们写东西从来不动脑子,不是写出来就完了,要高效 : SELECT ID, : SUM(CASE WHEN CALLRESULT IS NULL THEN 0 ELSE 1 END) AS CALLFLAG : FROM HISTORYTABLE : GROUP BY ID : HAVING CALLFALG = 0
|
B*****g 发帖数: 34098 | 8 而且好像不能去掉dup id(这个需要检验一下)
【在 c*******e 的大作中提到】 : 而且not in是一种比较危险的写法,万一那个 : subquery里面id有null的话,结果就不对了
|
e*u 发帖数: 17 | 9 Have you ever checked your query plan? or even your query?
At least yours is not correct in SQL Server.
【在 w*r 的大作中提到】 : ft... : 我回答一下好了,你们写东西从来不动脑子,不是写出来就完了,要高效 : SELECT ID, : SUM(CASE WHEN CALLRESULT IS NULL THEN 0 ELSE 1 END) AS CALLFLAG : FROM HISTORYTABLE : GROUP BY ID : HAVING CALLFALG = 0
|
e*u 发帖数: 17 | 10 if there is null in the ID column, something has already been wrong.
No matter how you write the query.
【在 c*******e 的大作中提到】 : 而且not in是一种比较危险的写法,万一那个 : subquery里面id有null的话,结果就不对了
|
|
|
B*****g 发帖数: 34098 | 11 use not exists should not cause the null problem.
【在 e*u 的大作中提到】 : if there is null in the ID column, something has already been wrong. : No matter how you write the query.
|
B*****g 发帖数: 34098 | 12 ding
【在 e*u 的大作中提到】 : Have you ever checked your query plan? or even your query? : At least yours is not correct in SQL Server.
|
e*u 发帖数: 17 | 13 Well, I don't think "not exists" can get anything different from
"not in" in case of NULL. Maybe you can write your query out.
Also, I don't this solve the problem.
If the ID column is NULLable, and the user want to get the NULL out
for whatever purpose, there has to be something wrong. What a NULL
stands for?
【在 B*****g 的大作中提到】 : use not exists should not cause the null problem.
|
d*******8 发帖数: 3182 | 14 思路严谨,但如果id允许null的话,可以让dba卷铺盖回家了。
【在 c*******e 的大作中提到】 : 而且not in是一种比较危险的写法,万一那个 : subquery里面id有null的话,结果就不对了
|
d*******8 发帖数: 3182 | 15 这不是sql query,楼主并没有阐明是oracle环境。
不可否认,在oracle环境中,这份方案是很不错的。
【在 w*r 的大作中提到】 : ft... : 我回答一下好了,你们写东西从来不动脑子,不是写出来就完了,要高效 : SELECT ID, : SUM(CASE WHEN CALLRESULT IS NULL THEN 0 ELSE 1 END) AS CALLFLAG : FROM HISTORYTABLE : GROUP BY ID : HAVING CALLFALG = 0
|
n********6 发帖数: 1511 | 16
【在 d*******8 的大作中提到】 : 思路严谨,但如果id允许null的话,可以让dba卷铺盖回家了。
|
B*****g 发帖数: 34098 | 17 在俺们公司不会发生,DBA永远是对的。呵呵
【在 d*******8 的大作中提到】 : 思路严谨,但如果id允许null的话,可以让dba卷铺盖回家了。
|
w*r 发帖数: 2421 | 18 我不需要check plan, 这个plan我有脚都想得出来是什么样子,至于你说的什么SQL
server, 请check ANSI SQL ,这种syntax的问题不是我讨论的重点
【在 e*u 的大作中提到】 : Have you ever checked your query plan? or even your query? : At least yours is not correct in SQL Server.
|
B*****g 发帖数: 34098 | 19 如果我没记错,not exists 可以return null。当然performance很烂。
select * from tbl a
where not exists
(select b.id from tbl b where b.id = a.id and b.callresult is not null)
【在 e*u 的大作中提到】 : Well, I don't think "not exists" can get anything different from : "not in" in case of NULL. Maybe you can write your query out. : Also, I don't this solve the problem. : If the ID column is NULLable, and the user want to get the NULL out : for whatever purpose, there has to be something wrong. What a NULL : stands for?
|
w*r 发帖数: 2421 | 20 not exists在oracle/db2/teradata 上面优化得都很烂,netezza还成,
SQL server这种没有optimizer的RDBMS不谈也罢
【在 B*****g 的大作中提到】 : use not exists should not cause the null problem.
|
|
|
B*****g 发帖数: 34098 | 21 peng,哈哈。上一篇俺刚说了performance很烂。
【在 w*r 的大作中提到】 : not exists在oracle/db2/teradata 上面优化得都很烂,netezza还成, : SQL server这种没有optimizer的RDBMS不谈也罢
|
w*r 发帖数: 2421 | 22 这个table如果1B row就等吧………………
【在 B*****g 的大作中提到】 : 如果我没记错,not exists 可以return null。当然performance很烂。 : select * from tbl a : where not exists : (select b.id from tbl b where b.id = a.id and b.callresult is not null)
|
w*r 发帖数: 2421 | 23 beijing, 你换工作了没?有找到好工作的话给我提个醒,正在hesitate要不要dump现
在的employer
【在 B*****g 的大作中提到】 : peng,哈哈。上一篇俺刚说了performance很烂。
|
B*****g 发帖数: 34098 | 24 没有,找个好工作不容易。
【在 w*r 的大作中提到】 : beijing, 你换工作了没?有找到好工作的话给我提个醒,正在hesitate要不要dump现 : 在的employer
|
c*******e 发帖数: 8624 | 25 也不是不可能,况且这种情况是id,另外一个情况也许就是平常
一个field
【在 d*******8 的大作中提到】 : 思路严谨,但如果id允许null的话,可以让dba卷铺盖回家了。
|
j*****n 发帖数: 1781 | 26 俺要去买豆腐!
【在 w*r 的大作中提到】 : not exists在oracle/db2/teradata 上面优化得都很烂,netezza还成, : SQL server这种没有optimizer的RDBMS不谈也罢
|
w*r 发帖数: 2421 | 27 豆腐,给你
【在 j*****n 的大作中提到】 : 俺要去买豆腐!
|
j*****n 发帖数: 1781 | 28 你直接砸好了,省得俺费力去撞...
【在 w*r 的大作中提到】 : 豆腐,给你
|
B*****g 发帖数: 34098 | 29 砸
【在 j*****n 的大作中提到】 : 你直接砸好了,省得俺费力去撞...
|