集合函數 collect_set() collect_list()
實驗數據1
userid | username |
---|---|
11101 | 張三 |
11101 | 李四 |
11101 | 王五 |
11101 | 趙六 |
11101 | 張三 |
注意到張三出現了兩次
-- 建表語句
create table temp.strategy_temp_20200813_function_test (
userid string,
username string
)row format delimited fields terminated by ',' STORED AS TEXTFILE
-- 查看
select * from temp.strategy_temp_20200813_function_test t
collect_set()
collect_set()
通常用於列轉行,將某一個列轉換成為一行且去重。
-- 去重的合並
select userid, collect_set(username) username
from temp.strategy_temp_20200813_function_test t
group by userid
結果是
user_id | username |
---|---|
11101 | ["張三","李四",”王五","趙六"] |
若要不去重,則需要使用 collect_list()
, 若需要對合並內容排序則使用 group_concat()
collect_list()
collect_list()
通常用於列轉行, 將某一列合並后,轉換成一行,不去重。
-- 去重的合並
select userid, collect_list(username) username
from temp.strategy_temp_20200813_function_test t
group by userid
結果是
user_id | username |
---|---|
11101 | ["張三","李四",”王五","趙六","張三"] |