, v2p AS (
SELECT DISTINCT video_asin, product_asin
from video_table
where video_asin IN (
‘ABC’,
‘CDF’,
‘DEF’,
‘FRW’)
)
, video_data AS (
SELECT distinct
start_date::date,
video_view,
video_asin,
page_asin
FROM video_metrics
WHERE
page_asin IN (select DISTINCT product_asin from v2p)
AND start_date between TO_DATE('01/26/2018','MM-DD-YYYY') and TO_DATE('02/04/2018','MM-DD-YYYY') + 0.9999
)
select sum(video_view) from video_data;我正在运行简单的查询,但是我在使用IN而不是IN得到奇怪的结果。
(1)。当我做page_asin IN (select DISTINCT product_asin from v2p),时,我得到了1,109,567作为select sum(video_view) from video_data;
(2)。当我执行相同的查询,但在page_asin NOT IN (select DISTINCT product_asin from v2p),中使用NOT时,我将得到7,032,405作为sum。
(3)。如果删除整行(page_asin IN (select DISTINCT product_asin from v2p),)以获得所有结果,我将得到8,148,803.作为和。
但我假设,如果我加上(1)和(2),我应该得到(3)。但实际上,我得到的是:1,109,567 + 7,032,405 = 8,141,972,而不是8,148,803, (3)。
为什么会这样?为什么我错过了7000个视频视图?
发布于 2018-05-15 16:35:16
NULL值既不是IN,也不是列表的NOT IN。
因此,某些行上的page_asin必须是NULL。
注意:select distinct在子查询中是多余的。没有理由把它包括在内。
https://stackoverflow.com/questions/50354907
复制相似问题