给定一个表temp,字段是 user_id,clo1,col2…col12 12各字段代表12个月电费,求最终结果展现:user_id,month,money
with temp as (select 1 as user_id,212 as 1month,432 as 2month,887 as 3month) select user_id,a.month,a.money from temp lateral view explode(map('Jar',1month,'Sec',2month,'March',3month)) a as month,money;
原数据:
结果数据:
巧妙使用map函数将月份和电费处理为key,value的数据结构,再通过explode炸裂函数获取每个元素对应的key和value值并展示为一行数据
参考资料:
添加链接描述