[英]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:我想要 pivot 如下所示:
Specifically, I'd like:具体来说,我想:
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_area
和feature
以及组中的 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![]() |
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![]() |
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.