ETL_场景练习

1 行列转换

(1) 列拆分为多行

把指定字段按指定分隔符进行拆分为多行,然后其它字段直接复制.

sql 复制代码
select
     字段列
    ,hobby2
from tbl
-- lateral view udtf(expression) tablealias as columnalias (',' columnalias)
lateral view explode(split(hobby,';')) temp as hobby2

(2) 行扁平化

把同一组的多行数据合并成为一行.

sql 复制代码
select 
  id, 
  name, 
  age, 
  split(concat_ws(':',collect_list(hobby)),':')[0] as hobby1,
  split(concat_ws(':',collect_list(hobby)),':')[1] as hobby2,
  split(concat_ws(':',collect_list(hobby)),':')[2] as hobby3
from tbl
group by id, name, age;

(3) 列转行

如果数据一列有相同的值,按照指定的字段,将其中一列的字段内容变成不同的列,然后把多行数据转换为一行数据.

sql 复制代码
select
    name
   ,max(if(wek = '周一',work_hour,0)) as mon
   ,max(if(wek = '周二',work_hour,0)) as tue
   ,max(if(wek = '周三',work_hour,0)) as wed
   ,max(if(wek = '周四',work_hour,0)) as thu
   ,max(if(wek = '周五',work_hour,0)) as fi
   ,max(if(wek = '周六',work_hour,0)) as sat
   ,max(if(wek = '周日',work_hour,0)) as sun
from tbl
group by name

(4) 行转列

把数据字段的字段名转换为一列,把数据行变为数据列.

复制代码
select name, 'mon' as wek, mon as work_hour from tbl
union all
select name, 'tue' as wek, tue as work_hour from tbl
union all
select name, 'wed' as wek, wed as work_hour from tbl
union all
select name, 'thu' as wek, thu as work_hour from tbl
union all
select name, 'fri' as wek, fri as work_hour from tbl
union all
select name, 'sat' as wek, sat as work_hour from tbl
union all
select name, 'sun' as wek, sun as work_hour from tbl
相关推荐
7***68431 小时前
Spring Boot 从 2.7.x 升级到 3.3注意事项
数据库·hive·spring boot
笨蛋少年派1 小时前
跨境电商大数据分析系统案例:③建模、分析与暂时收尾
hive·数据挖掘·数据分析
yumgpkpm2 小时前
腾讯TBDS和Cloud Data AI CMP 比较的缺陷在哪里?
hive·hadoop·elasticsearch·zookeeper·spark·kafka·hbase
K***658915 小时前
从0到1部署Tomcat和添加servlet(IDEA2024最新版详细教程)
hive·servlet·tomcat
李兆龙的博客16 小时前
从一到无穷大 #61 Hive史诗
数据仓库·hive·hadoop
q***06471 天前
Spring Boot 从 2.7.x 升级到 3.3注意事项
数据库·hive·spring boot
心止水j1 天前
hive分区
数据仓库·hive·hadoop
心止水j1 天前
Hive 桶表的创建、数据导入、查询与导出
数据仓库·hive·hadoop
8***a8152 天前
从0到1部署Tomcat和添加servlet(IDEA2024最新版详细教程)
hive·servlet·tomcat
池塘水悠悠4 天前
kettle 变量-大坑
etl·kettle·kettel变量