1. concat
說明 : 將多個字符串 按照默認分隔符(空) ,拼接成一個字符串
示例 : select concat('gao','cun');
結果 : gaocun
2. concat_ws
說明 : 將多個字符串 或者array, 按照指定分隔符(第一個參數), 拼接成一個字符串
示例 : select concat_ws('-','gao','cun',split('da,wang',','));
結果 : gao-cun-da-wang
3. collect_set (聚合函數 UDAF)
說明 : 將分組內 指定字段封裝成一個set(對元素去重) 返回
示例 :
4. collect_list (聚合函數 UDAF)
說明 : 將分組內 指定字段封裝成一個list(對元素不去重) 返回
示例 :
5. group_concat (mysql 中函數)
說明 : group_concat( [distinct] 要連接的字段 [order by 排序字段 asc/desc ] [separator ‘分隔符’] )
示例 : select name
, group_concat(distinct friend order by friend desc separator '@') as friends1
, group_concat(friend) as friends2
from (
select '劉備' as name, '張飛' as friend
union all
select '劉備' as name, '張飛' as friend
union all
select '劉備' as name, '關羽' as friend
union all
select '劉備' as name, '趙雲' as friend
union all
select '曹操' as name, '許褚' as friend
union all
select '曹操' as name, '荀彧' as friend
) as t1
group by name;
-- 結果
name friends1 friends2
劉備 趙雲@張飛@關羽 張飛,張飛,關羽,趙雲
曹操 許褚@荀彧 許褚,荀彧
6. 行轉列
-- 人員表
name friend
劉備 張飛
劉備 張飛
劉備 關羽
劉備 趙雲
曹操 許褚
曹操 荀彧
-- 統計結果
name friends
劉備 張飛、關羽、趙雲
曹操 許褚、荀彧
-- 執行sql
with t1 as (
select '劉備' as name,'張飛' as friend union all
select '劉備' as name,'張飛' as friend union all
select '劉備' as name,'關羽' as friend union all
select '劉備' as name,'趙雲' as friend union all
select '曹操' as name,'許褚' as friend union all
select '曹操' as name,'荀彧' as friend )
select
name
,concat_ws(',', collect_set(friend)) as friends1 -- 對元素去重
,concat_ws(',', collect_list(friend)) as friends2 -- 對元素不去重
from t1
group by name;
-- 結果
name friends1 friends2
劉備 張飛,關羽,趙雲 張飛,張飛,關羽,趙雲
曹操 許褚,荀彧 許褚,荀彧