本文列举在一些特殊需求下,比如需要把多行数据根据某一列的字段值转化为字段名的多行转一行的需求,或者把字段名转化为字段值的一行转多行的需求
有点拗口,且看两个实例:
将下表
转化成这样
select name ,info['语文'] as Chinese ,info['数学'] as Math ,info['英语'] as English from (select name,str_to_map(concat_ws(',',collect_set(concat_ws(':',subject,cast(score as string))))) as info from test group by name ) a
方法二:利用case when函数
select name ,max(case when subject = '语文' then score else 0 end) as Chinese ,max(case when subject = '数学' then score else 0 end) as Math ,max(case when subject = '英语' then score else 0 end) as English from test group by name需求二:字段名转化为字段值
转化成
select a.date ,b.label ,b.value from (select * from daily_report ) a LATERAL VIEW explode (map( 'UV', uv ,'新增UV', newuv ,'视频存量', video ,'新增视频', newvideo ,'播放量', vv ,'会员数', vip_num ,'新增会员数', new_vip_num )) b as label, value
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)