hive sql 行列转置

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


原始数据

如上图,将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 行列转置)