美文网首页
hive sql 行列转置

hive sql 行列转置

作者: kevie | 来源:发表于2020-04-27 16:03 被阅读0次

    最近在做大数据的建模遇到的个需求,需要将行转置列,记录下。


    原始数据

    如上图,将datatime相同的多行变成一行数据,效果如下图。


    处理后数据
    废话不多说,上代码:
    select datatime,
    concat_ws('',collect_set(item1)) as '填报人',
    concat_ws('',collect_set(item2)) as '填报部门',
    concat_ws('',collect_set(item3)) as '填报内容',
    concat_ws('',collect_set(item4)) as '审核人'
    from (
      select datatime,
      case when item_name ='填报人' then value else '' end as item1,
      case when item_name ='填报部门' then value else '' end as item2,
      case when item_name ='填报内容' then value else '' end as item3, 
      case when item_name ='审核人' then value else '' end as item4
      from tablename
      ) a
      group by datatime
    

    相关文章

      网友评论

          本文标题:hive sql 行列转置

          本文链接:https://www.haomeiwen.com/subject/pxxuwhtx.html