繁体   English   中英

快速旋转数据

[英]Presto Pivoting Data

我是 Presto 的新手,在其中旋转数据时遇到问题。 我使用的方法如下:

select
distinct location_id,
case when role_group = 'IT' then employee_number end as IT_emp_num,
case when role_group = 'SC' then employee_number end as SC_emp_num,
case when role_group = 'HR' then employee_number end as HR_emp_num
from table
where 1=1
and id = 1234

这很好,但是,也为行填充了 null 值,我想 pivot 数据,只返回包含相关信息的一行。

在此处输入图像描述

我试过使用 array_agg function,它会折叠数据,但它也会保留 null 值(例如,它会为第一列返回null,301166,null

如果每个位置只需要一行,您可以将max与 group by 一起使用:

select location_id, 
  max(IT_emp_num) IT_emp_num, 
  max(SC_emp_num) SC_emp_num, 
  max(HR_emp_num) HR_emp_num
from (
  select location_id,
    case when role_group = 'IT' then employee_number end as IT_emp_num,
    case when role_group = 'SC' then employee_number end as SC_emp_num,
    case when role_group = 'HR' then employee_number end as HR_emp_num
  from table
  where id = 1234)
group by location_id

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM