繁体   English   中英

使用 Presto 旋转 SQL

[英]Pivoting with Presto SQL

我有一组记录显示软件组件的实施状态。 每行都有一个特性区域、该区域内的一个特性、出现该特性的产品及其实现状态(绿色、红色或黄色),如下所示:

特征_区域 特征 产品 状态
用户体验 报名 应用程序1 绿色的
用户体验 报名 应用程序2 红色的
用户体验 注销 应用程序1 绿色的
用户体验 注销 应用程序2 红色的
后端 更新用户 应用程序3 绿色的
后端 删除用户 应用程序3 红色的

我想要 pivot 如下所示: 在此处输入图像描述

具体来说,我想:

  1. 每个功能的一行和每个应用程序的列显示该功能的状态
  2. 不要在第一列中一遍又一遍地重复相同的特征区域值

我尝试使用下面的方法,这让我为每个应用程序获得了一个专栏。 我无法弄清楚如何正确分组以获得我正在寻找的 output。 我欢迎任何想法。

case when Product = 'App1' then title end as App1,
case when Product = 'App2' then title end as App2,
case when Product = 'App3' then title end as App3

根据提供的描述,我建议按feature_areafeature以及组中的 select 任意值进行分组(或者如果您预期不同的值,则可能array_agg )(注意,我使用 Presto/Trino specificif来获得简洁的语法而不是case ):

-- sample data
with dataset(Feature_Area, Feature, Product, Status) as (
    values ('User experience', 'Sign Up', 'App1', 'Green'),
        ('User experience', 'Sign Up', 'App2', 'Red'),
        ('User experience', 'Log off', 'App1', 'Green'),
        ('User experience', 'Log off', 'App2', 'Red'),
        ('Back End', 'Update User', 'App3', 'Green'),
        ('Back End', 'Delete User', 'App3', 'Red')
)

-- query
select Feature_Area,
       Feature,
       arbitrary(if(Product = 'App1', Status)) App1,
       arbitrary(if(Product = 'App2', Status)) App2,
       arbitrary(if(Product = 'App3', Status)) App3
from dataset
group by Feature_Area, Feature
order by Feature_Area desc, Feature desc;

Output:

特征_区域 特征 应用程序1 应用程序2 应用程序3
用户体验 报名 绿色的 红色的 NULL
用户体验 注销 绿色的 红色的 NULL
后端 更新用户 NULL NULL 绿色的
后端 删除用户 NULL NULL 红色的

暂无
暂无

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

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