Presto 数据透视

发布于 2025-01-13 17:09:44 字数 602 浏览 1 评论 0原文

我对 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

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

输入图片这里的描述

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

I am really new to Presto and having trouble pivoting data in it.
The method I am using is the following:

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

This is fine, however, null values are also populated for the rows and I would like to pivot the data, to only return one row with the relevant info.

enter image description here

I have tried using the array_agg function, which will collapse the data but it also keeps the null values (e.g. it will return null,301166,null for the first colum)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

謸气贵蔟 2025-01-20 17:09:44

如果每个位置只需要一行,您可以使用 max 与分组依据:

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

If only one row per location is needed you can use max with 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文