在红移中使用 DISTINCT 的 listagg

listagg with DISTINCT in redshift

我正在努力寻找一种在红移 listagg 中执行 DISTINCT 的好方法。

我只想列出产品组合,但每一行都应该 return 不同产品的列表。

例子

期望的输出:

bulb, light
bulb, light, fan

而不是:

bulb, bulb, light
bulb, bulb, light, fan

下面是我的SQL:

select
    tit.listagg  
from (
    SELECT
        username,
        listagg(node_name, ',')
        WITHIN GROUP (ORDER BY node_name asc)
    FROM table
    Where node_type not like '%bla bla%'
    GROUP BY username
) as tit
group by listagg;

您可以枚举行,然后 select 第一个:

select username,
       listagg(case when seqnum = 1 then node_name end, ',') within group (order by node_name asc) 
from (select t.*,
             row_number() over (partition by username, node_name order by node_name) as seqnum
      from table t
      where node_type not like '%bla bla%' 
     ) t
group by username;

这使用 listagg() 忽略 NULL 值的功能。

Redshift 现在支持 LISTAGG DISTINCT,因此不需要子查询:https://aws.amazon.com/about-aws/whats-new/2017/10/amazon-redshift-announces-support-for-listagg-distinct/