簡體   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