avatar
包子请教query# Database - 数据库
m*2
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的不包括
在内。
这个应该怎样写?
谢谢啦!
avatar
e*u
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的不包括

avatar
d*8
3
select ID from table where ID not in (select ID from table where callresult
not null)?
avatar
d*8
4
en,打字打了5分钟

callresult

【在 d*******8 的大作中提到】
: select ID from table where ID not in (select ID from table where callresult
: not null)?

avatar
m*2
5
en .... 刚才突然短路。。。
OK. Got!

callresult

【在 d*******8 的大作中提到】
: select ID from table where ID not in (select ID from table where callresult
: not null)?

avatar
w*r
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

avatar
c*e
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

avatar
B*g
8
而且好像不能去掉dup id(这个需要检验一下)

【在 c*******e 的大作中提到】
: 而且not in是一种比较危险的写法,万一那个
: subquery里面id有null的话,结果就不对了

avatar
e*u
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

avatar
e*u
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的话,结果就不对了

avatar
B*g
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.

avatar
B*g
12
ding

【在 e*u 的大作中提到】
: Have you ever checked your query plan? or even your query?
: At least yours is not correct in SQL Server.

avatar
e*u
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.
avatar
d*8
14
思路严谨,但如果id允许null的话,可以让dba卷铺盖回家了。

【在 c*******e 的大作中提到】
: 而且not in是一种比较危险的写法,万一那个
: subquery里面id有null的话,结果就不对了

avatar
d*8
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

avatar
n*6
16


【在 d*******8 的大作中提到】
: 思路严谨,但如果id允许null的话,可以让dba卷铺盖回家了。
avatar
B*g
17
在俺们公司不会发生,DBA永远是对的。呵呵

【在 d*******8 的大作中提到】
: 思路严谨,但如果id允许null的话,可以让dba卷铺盖回家了。
avatar
w*r
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.

avatar
B*g
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?

avatar
w*r
20
not exists在oracle/db2/teradata 上面优化得都很烂,netezza还成,
SQL server这种没有optimizer的RDBMS不谈也罢

【在 B*****g 的大作中提到】
: use not exists should not cause the null problem.
avatar
B*g
21
peng,哈哈。上一篇俺刚说了performance很烂。

【在 w*r 的大作中提到】
: not exists在oracle/db2/teradata 上面优化得都很烂,netezza还成,
: SQL server这种没有optimizer的RDBMS不谈也罢

avatar
w*r
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)

avatar
w*r
23
beijing, 你换工作了没?有找到好工作的话给我提个醒,正在hesitate要不要dump现
在的employer

【在 B*****g 的大作中提到】
: peng,哈哈。上一篇俺刚说了performance很烂。
avatar
B*g
24
没有,找个好工作不容易。

【在 w*r 的大作中提到】
: beijing, 你换工作了没?有找到好工作的话给我提个醒,正在hesitate要不要dump现
: 在的employer

avatar
c*e
25
也不是不可能,况且这种情况是id,另外一个情况也许就是平常
一个field

【在 d*******8 的大作中提到】
: 思路严谨,但如果id允许null的话,可以让dba卷铺盖回家了。
avatar
j*n
26
俺要去买豆腐!

【在 w*r 的大作中提到】
: not exists在oracle/db2/teradata 上面优化得都很烂,netezza还成,
: SQL server这种没有optimizer的RDBMS不谈也罢

avatar
w*r
27
豆腐,给你

【在 j*****n 的大作中提到】
: 俺要去买豆腐!
avatar
j*n
28
你直接砸好了,省得俺费力去撞...

【在 w*r 的大作中提到】
: 豆腐,给你
avatar
B*g
29


【在 j*****n 的大作中提到】
: 你直接砸好了,省得俺费力去撞...
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。