简体   繁体   中英

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

I'd like to pivot this as shown here: 在此处输入图像描述

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. 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 ):

-- 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:

Feature_Area Feature App1 App2 App3
User experience Sign Up Green Red NULL
User experience Log off Green Red NULL
Back End Update User NULL NULL Green
Back End Delete User NULL NULL Red

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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