- 1. Hive 多行转单列,即行转列
一般行转列,Hive用concat_ws指定分隔符来拼接字符串,如果需要去重的话,还需要用collect_set,如果不需要去重,则可以用collect_list替代。
模拟代码
select
t1.ID,
concat_ws(';',collect_set(cast(t1.name as string))) name
from
user_list t1
group by ID
- 2. Oracle的多行转单列,即行转列
Oracle的行转列一般用LISTAGG WITHIN GROUP 来拼接字符串。如果需要去重,可以使用distinct来去重。代码如下:
模拟代码
select
t1.ID,
LISTAGG(t1.name, ';') WITHIN GROUP(ORDER BY t1.排序字段)
from
user_list t1
group by ID