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

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

 

 

我们的方法是 , 用 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

 

相关文章:

  • 2022-12-23
  • 2022-02-22
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-26
猜你喜欢
  • 2022-12-23
  • 2021-11-12
  • 2021-12-22
  • 2022-01-20
  • 2022-12-23
  • 2022-12-23
  • 2022-01-08
相关资源
相似解决方案