选择ID不在另一个表中的ID不起作用?

时间:2017-09-07 13:39:17

标签: sql vertica

我使用的是Vertica SQL

两张桌子:

Table1
ID,Sales
1,50 
2,0
3,60
..

Table2
ID,sales
3,50
4,55
5,60
...

select count(distinct id) from table1 where sales >0; 
--returns about 50
select count(distinct id) from table2 where sales >0; 
--returns about 20

select count(distinct t1.id) from table1 t1 where t1.sales >0 and t1.id not in (select distinct(t2.id) from table2 t2 where t2.sales >0);
--returns 0

如果table1在给定条件下的记录多于table2,那怎么可能呢?

ID是varchar,销售额是数字

提前致谢!

2 个答案:

答案 0 :(得分:4)

这是您的查询:

select count(distinct t1.id)
from table1 t1
where t1.sales > 0 and
      t1.id not in (select distinct t2.id from table2 t2 where t2.sales > 0);

not in是一个危险的构造。如果子查询中的任何行的NULL值为id,则外部查询将返回 no 行。出于这个原因,我强烈建议使用not exists来表示这样的逻辑:

select count(distinct t1.id)
from table1 t1
where t1.sales > 0 and
      not exists (select 1from table2 t2 where t2.id = t1.id and t2.sales > 0);

答案 1 :(得分:0)

您也可以通过加入来执行此操作:

SELECT COUNT(DISTINCT t1.id) as id_count
FROM table1 t1
LEFT JOIN
(SELECT DISTINCT id
 FROM table2
 WHERE sales > 0) sub
ON t1.id = sub.id
WHERE sub.id IS NULL
AND t1.sales > 0

它看起来并不漂亮,但可能会有性能优势。

相关问题