"NOT IN"/"NOT EXISTS" 用于成对列比较在 Redshift 中不起作用

"NOT IN"/"NOT EXISTS" for pairwise column comparison not working in Redshift

我想用 redshift 重写下面的语句。 Redshift 不支持 IN/NOT IN 进行多列比较。当我也使用 NOT EXISTS 尝试相同的查询时,出现以下错误:

无效操作:由于内部错误,不支持此类相关子查询模式;

select count(distinct item) from tbl1  
where (item,store) not in (
select distint item, store form tbl1 
where *store changed* -- some filters
)

您的 Redshift 版本不支持这种元组语法。您可以改写如下:

SELECT COUNT(DISTINCT item) AS cnt
FROM tbl1 t1
WHERE NOT EXISTS (
    SELECT 1
    FROM tbl1 t2
    WHERE t2.item = t1.item AND
          t2.store = t1.store AND
          (other filters here...)
);

Tim 的想法似乎很酷,但您也可以使用以下代码:

select     count(distinct t1.item)
from       tbl1 t1
left join
(
        select distinct item, store
        form   tbl1 
        where  *store changed* -- some filters
) t2
on t2.item = t1.item
and t2.store = t1.store

where t2.item is null
and   t2.store is null