Sequel替代集合的解决方案

时间:2018-09-11 22:48:02

标签: sql oracle

立即查询

CREATE TABLE collect_char_wk1 STORED AS ORC AS
SELECT cluster, COLLECT_SET(characteristic)
FROM timeperiod1
GROUP BY cluster;

样本数据

cluster  characteristic
 A        1
 A        2
 A        3
 B        2
 B        3

预期结果

cluster  characteristic
 A        [1,2,3]
 B        [2,3]

当前这与上面的查询一起使用,但是,我希望它使用类似的功能在sql或plsql中编写,因为我们在sql中没有直接的collect_set。 请告诉我是否有任何方法可以做到?谢谢

1 个答案:

答案 0 :(得分:0)

看看Oracle aggregation techniques

您要查找的是 LISTAGG 。这就是您可能遇到的情况

select c1, LISTAGG(c2, ',') WITHIN GROUP (ORDER BY 1)  from (
     select      'A' c1,         1 c2 from dual union all
     select      'A' c1,2 c2 from dual union all
     select      'A' c1,3 c2 from dual union all
     select      'B' c1,2 c2 from dual union all
     select      'B' c1,3 c2 from dual 
) group by c1 ;

(请注意,这是一种SQL 解决方案)