一、列轉行 (對某列拆分,形成新列)
使用函數:lateral view explode(split(column, ',')) num
eg: 如表:t_row_to_column_tmp 數據如下,對tag列進行拆分
SQL代碼:
select id,tag,tag_new
from t_row_to_column_tmp
lateral view explode(split(tag, ',')) num as tag_new
where id=212022894;
二、行轉列 (根據主鍵,對某列進行合並)
使用函數:concat_ws(',',collect_set(column))
說明:collect_list 不去重,collect_set 去重。 column 的數據類型要求是 string
eg:如表:t_column_to_row ,根據id,對tag_new 進行合並
SQL代碼1:
select id,
concat_ws(',',collect_set(tag_new)) as tag_col
from t_column_to_row
group by id;
SQL代碼2:
select id,
concat_ws(',',collect_list(tag_new)) as tag_col
from t_column_to_row
group by id;