Hive 行列转换

行列转换
列转行

使用 lateral view + explode(array|map)lateral view + inline(array_struct) 可以将列转换为行。

  • 单列转多行,降维(单列数组或键值对)

示例1:explode(array(...))

sql 复制代码
select ..., A
from T
lateral view explode(ARRAY_FIELD) as A;
sql 复制代码
select explode(`array`(88.2,98.3,67.1)) AS (price);

示例2:explode(map(...))

sql 复制代码
select ..., K, V
from T
lateral view explode(MAP_FIELD) as K, V;
sql 复制代码
select explode(`map`("java",56,"mysql",88,"javascript",66)) AS (subject, score);

示例3:inline(array_struct)

sql 复制代码
select ..., 
from T
lateral view inline(STRUCT_ARRAY_FIELD)V as F1,...,FN;
sql 复制代码
with tmp as (
select array(
	named_struct('name','henry','age',22,'is_member','true'),
	named_struct('name','pola','age',20,'is_member','true'),
	named_struct('name','ariel','age',19,'is_member','true')
   ) AS array_struct
)
select name,age,is_member
from tmp
lateral view inline(array_struct)V as name,age,is_member;

lateral view inline(array_struct)将结构体数组的每个元素都转化为一行,每一行都包含结构体字段的值.

前:

后:

  • 多列转多行
sql 复制代码
select ..., A
from T
lateral view explode(array|map(F1,...,FN))V as A;

示例:

sql 复制代码
SELECT name, class, Scores.subject, Scores.score
FROM Students
LATERAL VIEW EXPLODE(ARRAY(
	named_struct('subject','math','score',math_score),
	named_struct('subject','science','score',science_score)
	)
) V AS Scores;

前:

后:

行转列
  • 多行转多列
    条件聚合,通常用于将多行数据中满足条件的某个值聚合到单个行中。
sql 复制代码
select
		F1,...,
		sum(if(C1,0,V1)) as A1,
		sum(if(C2,0,V2)) as A2,
		sum(if(C3,0,V3)) as A3
	from TABLE_NAME
	group by F1,...
	
	drop table if exists lateral_view_stack_test1w;
	create table lateral_view_stack_test1w as
	select year,
		   sum(if(month(order_time)=1,order_amount,0)) as sum_jan,
		   sum(if(month(order_time)=2,order_amount,0)) as sum_feb,
		   sum(if(month(order_time)=3,order_amount,0)) as sum_mar,
		   sum(if(month(order_time)=4,order_amount,0)) as sum_apr,
		   sum(if(month(order_time)=5,order_amount,0)) as sum_may,
		   sum(if(month(order_time)=6,order_amount,0)) as sum_jun,
		   sum(if(month(order_time)=7,order_amount,0)) as sum_jul,
		   sum(if(month(order_time)=8,order_amount,0)) as sum_aug,
		   sum(if(month(order_time)=9,order_amount,0)) as sum_sep,
		   sum(if(month(order_time)=10,order_amount,0)) as sum_oct,
		   sum(if(month(order_time)=11,order_amount,0)) as sum_nov,
		   sum(if(month(order_time)=12,order_amount,0)) as sum_dec
	from hive_internal_par_regex_test1w
	where year>=2014
	group by year;
相关推荐
ghgxm52019 小时前
EXCEL使用VBA代码实现按条件查询数据库--简单实用
开发语言·数据仓库·笔记·excel·数据库开发
喻师傅2 天前
Hive 中 NULL 值在逻辑判断中的“陷阱”(踩坑复盘)
数据仓库·hive·hadoop
涤生大数据2 天前
放弃Canal后,我们用Flink CDC实现了99.99%的数据一致性
大数据·数据仓库·flink·大数据开发·flink cdc·数据开发·实时数据
jinxinyuuuus2 天前
订阅指挥中心:数据可移植性、Schema设计与用户数据主权
数据仓库·人工智能
老徐电商数据笔记3 天前
技术复盘第四篇:Kimball维度建模在电商场景的实战应用
大数据·数据仓库·技术面试
程序员小羊!3 天前
数仓数据基线,在不借助平台下要怎么做?
大数据·数据仓库
Hello.Reader4 天前
Flink SQL 的 LOAD MODULE 深度实战——加载 Hive 模块、理解模块发现与常见坑
hive·sql·flink
老徐电商数据笔记4 天前
技术复盘第二篇:电商数据主题域划分企业级实践
大数据·数据库·数据仓库·零售·教育电商·技术面试
亲亲菱纱4 天前
hive数仓分层
数据仓库
老徐电商数据笔记4 天前
技术复盘第三篇:百果园新零售核心业务流程主题域划分详解
大数据·数据仓库·零售·技术面试