目录
一、行方向
1. 行方向的合并
1.1 concat 函数
1.2 concat_ws 函数
2. 行方向的拆分
二、列方向
1. 列方向的合并
1.1 group_concat 函数
1.2 collect_list 函数
1.3 collect_set 函数
2. 列方向的拆分
2.1 explode 函数
2.2 lateral view
-- 简单合并 select concat(column1, column2, column3) as merged_column from table_1; select concat('Hello', 'World'); -- HelloWorld -- NULL值的处理 select concat(NULL, 'Hello', 'World'); -- null select concat(nvl(olumn1, ''), column2, column3);
-- 带分隔符合并 select concat_ws(',', column1, column2, column3) as merged_column from table_1; select concat_ws('-', 'Hello', 'World'); -- Hello-World -- NULL值的处理 select concat_ws('-', NULL, 'Hello', 'World'); -- null select concat_ws(',', nvl(olumn1, ''), column2, column3);
-- 拆分成一个数组列 select split('wo shi xiao ming',' ') as new_column from table_1; -- ['wo','shi','xiao','ming'] -- 根据数组索引拆分成多列 select split('1_John','_')[0] as id ,split('1_John','_')[1] as name from table_1; -- 1 John
-- 默认分隔符合并 select name ,group_concat(course) as courses from table_1 group by name; -- Math,English -- 指定分隔符合并 select name ,group_concat(course, '_') as courses from table_1 group by name; -- Math_English
-- 合并为一个列表 select department ,collect_list(name) as name_list from table_1 group by department; -- ['John','Jack'] -- concat_ws 按照指定分隔符连接列表的值 select department ,concat_ws(',', collect_list(name)) as name_list from table_1 group by department; -- John,Jack
-- 合并为一个列表 select department ,collect_set(name) as name_list -- 假设有三行数据为'John', 'Jack', 'John' from table_1 group by department; -- ['John','Jack'] -- concat_ws 按照指定分隔符连接列表的值 select department ,concat_ws(',', collect_set(name)) as name_list from table_1 group by department; -- John,Jack
-- 拆分 array 类型 select explode(array_col) as new_col from table_1; -- 拆分成一列多行 -- 拆分 map 类型(map 是 key-value 结构) select explode(map_col) as (may_key_col, may_value_col) from table_1; -- 拆分成两列多行 -- 拆分 string 字符串类型 select explode(split(string_col, sep)) as new_col from table_name;
-- 错误示例 select other_col ,explode(array_col) as new_col from table_1;
select source_column ,new_column from table_1 lateral view explode(split(source_column, ',')) a as new_column; -- a是虚拟表名