环境 apache-hive-3.1.3
1、原始数据
select num, name
from test0422
where num is not null;

2、把相同num值的name合并
select num, concat_ws('|||', collect_list(name) )
from test0422
where num is not null
group by num;

3、 concat_ws 函数
- concat_ws 代表 concat with separator(分隔符),是 concat 的特殊形式。
- 语法格式:concat_ws(separator, str1, str2, …) 。
- 如有任何一个参数为 NULL ,则返回值为 NULL,可以考虑使用 nvl 函数将 NULL 替换为 ‘’ 。
select concat_ws(',', nvl(column1, ''), column2, column3);
4、collect_list 函数
- collect_list 函数用于将多个值收集到一个列表中,不去重。
- 语法格式:collect_list(expr) 。
- expr 可以是任意数据类型。但是外层有 concat_ws 时必须 cast(expr as string)。
5、collect_set 函数
- collect_set 函数用于将多个值收集到一个列表中,去重。
- 语法格式:collect_set(expr) 。
- expr 可以是任意数据类型。但是外层有 concat_ws 时必须 cast(expr as string)。
select num, concat_ws('-', collect_set(name) )
from test04222
where num is not null
group by num;
