按分组方式聚合字符串,并在hive和presto中排序

7gs2gvoe  于 2021-06-26  发布在  Hive
关注(0)|答案(1)|浏览(595)

我有一个表格,格式如下:

IDX IDY Time Text
idx1 idy1 t1 text1
idx1 idy2 t2 text2
idx1 idy2 t3 text3
idx1 idy1 t4 text4
idx2 idy3 t5 text5
idx2 idy3 t6 text6
idx2 idy1 t7 text7
idx2 idy3 t8 text8

我想看到的是这样的:

idx1 text1
idx1 text2, text3
idx1 text4
idx2 text5, text6
idx2 text7
idx2 text8

所以在最后阶段,我可以:

text1
text2, text3
text4
==SEPERATOR==
text5, text6
text7
text8

我怎样才能在Hive或普雷斯托?谢谢。

xytpbqjk

xytpbqjk1#

Hive
这是基本查询,如果您愿意,可以从这里获取它

select  IDX
       ,IDY
       ,min(time)                           as from_time
       ,max(time)                           as to_time
       ,concat_ws(',',collect_list (Text))  as text

from   (select  *
               ,row_number () over 
                (
                    partition by    IDX
                    order by        Time
                )   as rn
               ,row_number () over 
                (
                    partition by    IDX,IDY
                    order by        Time
                )   as rn_IDY

        from    mytable
        ) t

group by    IDX,IDY
           ,rn - rn_IDY

order by    IDX,from_time
+------+------+-----------+---------+-------------+
| idx  | idy  | from_time | to_time |    text     |
+------+------+-----------+---------+-------------+
| idx1 | idy1 | t1        | t1      | text1       |
| idx1 | idy2 | t2        | t3      | text2,text3 |
| idx1 | idy1 | t4        | t4      | text4       |
| idx2 | idy3 | t5        | t6      | text5,text6 |
| idx2 | idy1 | t7        | t7      | text7       |
| idx2 | idy3 | t8        | t8      | text8       |
+------+------+-----------+---------+-------------+

急板地

select  array_join(array_agg (Text),',')   as text

from   (select  *
               ,row_number () over 
                (
                    partition by    IDX
                    order by        Time
                )   as rn
               ,row_number () over 
                (
                    partition by    IDX,IDY
                    order by        Time
                )   as rn_IDY

        from    mytable
        ) t

group by    IDX,IDY
           ,rn - rn_IDY

order by    IDX,min(time)
;
+-------------+
|    text     |
+-------------+
| text1       |
| text2,text3 |
| text4       |
| text5,text6 |
| text7       |
| text8       |
+-------------+

相关问题