我在表格中有一个数据集。
id | attribute
-----------------
1 | a
2 | b
2 | a
2 | a
3 | c
期望的输出:
attribute| num
-------------------
a | 1
b,a | 1
c | 1
在 MySQL 中,我会使用:
select attribute, count(*) num
from
(select id, group_concat(distinct attribute) attribute from dataset group by id) as subquery
group by attribute;
我不确定这可以在 Redshift 中完成,因为它不支持 group_concat 或任何 psql 组聚合函数,如 array_agg() 或 string_agg()。看到这个问题。
另一种可行的解决方案是,如果我有办法从每个组中选择一个随机属性而不是 group_concat。这如何在 Redshift 中工作?