我有表格中的数据集。
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中如何运作?
受Masashi启发,此解决方案更简单,并且可以从Redshift中的组中选择一个随机元素。
SELECT id, first_value as attribute
FROM(SELECT id, FIRST_VALUE(attribute)
OVER(PARTITION BY id ORDER BY random()
ROWS BETWEEN unbounded preceding AND unbounded following)
FROM dataset)
GROUP BY id, attribute ORDER BY id;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句