Hive 窄表转宽表 , 长表转宽表

1.这是源数据以及需要转化的目标表.

我们的方法是 , 用 GROUP BY按照year分组 , 并且依次提取1月,2月,3月,4月的 num,具体实现

select year,
max(case when month=1 then money else 0 end) as M1,
max(case when month=2 then money else 0 end) as M2,
max(case when month=3 then money else 0 end) as M3,
max(case when month=4 then money else 0 end) as M4  
from sale group by year;

其中select year是选择年 , 配合后面GROUP BY 

中间的max是获取其中等于匹配到的值, 为什么要写个0 , 其实是为了去重 , 也是为了部位null

原文地址:https://www.cnblogs.com/alpha-cat/p/12957320.html