首页 > 解决方案 > 使用 hive 函数对数据进行排序

问题描述

我有一张蜂巢桌

create table abc ( id int, channel string, time int ); 

insert into table abc values
(1,'a', 12),
(1,'c', 10),
(1,'b', 15),
(2,'a', 15),
(2,'c', 12),
(2,'c', 7);

我希望结果表看起来像这样 -

id , journey
1, c->a->b
2, c->c->a

journey列按时间升序排列id

我努力了

select id , concat_ws(">", collect_list(channel)) as journey
from abc 
group by id

但它不保持秩序。

标签: hive

解决方案


使用子查询并按时间排序(以保留顺序),然​​后在外部查询中使用带有 group by 子句的 collect_list。

hive> select id , concat_ws("->", collect_list(channel)) as journey from 
      ( 
        select * from abc order by time
       )t 
        group by id;
    +-----+----------------+--+
    | id  |    journey     |
    +-----+----------------+--+
    | 1   | 'c'->'a'->'b'  |
    | 2   | 'c'->'c'->'a'  |
    +-----+----------------+--+

推荐阅读