简体   繁体   English

使用 Presto 旋转 SQL

[英]Pivoting with Presto SQL

I have a set of records showing the status of implementation of software components.我有一组记录显示软件组件的实施状态。 Each row has a feature area, a feature within that area, the product where that feature appears, and its implementation status (green, red, or yellow), like this:每行都有一个特性区域、该区域内的一个特性、出现该特性的产品及其实现状态(绿色、红色或黄色),如下所示:

Feature_Area特征_区域 Feature特征 Product产品 Status状态
User experience用户体验 Sign Up报名 App1应用程序1 Green绿色的
User experience用户体验 Sign Up报名 App2应用程序2 Red红色的
User experience用户体验 Log off注销 App1应用程序1 Green绿色的
User experience用户体验 Log off注销 App2应用程序2 Red红色的
Back End后端 Update User更新用户 App3应用程序3 Green绿色的
Back End后端 Delete User删除用户 App3应用程序3 Red红色的

I'd like to pivot this as shown here:我想要 pivot 如下所示: 在此处输入图像描述

Specifically, I'd like:具体来说,我想:

  1. A single row for each feature and columns for each app to show the status of that feature每个功能的一行和每个应用程序的列显示该功能的状态
  2. Don't repeat the same feature area value over and over in the first column不要在第一列中一遍又一遍地重复相同的特征区域值

I tried using the below, which got me a column for each app.我尝试使用下面的方法,这让我为每个应用程序获得了一个专栏。 I couldn't figure out how to group properly with this to get the output I was looking for.我无法弄清楚如何正确分组以获得我正在寻找的 output。 I'd welcome any ideas.我欢迎任何想法。

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

Based on provided description I suggest to group by feature_area and feature and select arbitrary value in group (or maybe array_agg if you anticipate different ones) (note, I used Presto/Trino specificif for concise syntax instead of case ):根据提供的描述,我建议按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: Output:

Feature_Area特征_区域 Feature特征 App1应用程序1 App2应用程序2 App3应用程序3
User experience用户体验 Sign Up报名 Green绿色的 Red红色的 NULL NULL
User experience用户体验 Log off注销 Green绿色的 Red红色的 NULL NULL
Back End后端 Update User更新用户 NULL NULL NULL NULL Green绿色的
Back End后端 Delete User删除用户 NULL NULL NULL NULL Red红色的

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

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