[英]How to create multiple columns based on the unique values in a single column? (presto SQL)
我在下面的示例中有以下数据。
account_id, campaign_objective, campaign_spend, conversions
__________, __________________, ______________, ___________
1, sales, 100, 25
1, brand, 50, 25
2, sales, 80, 12
2, brand, 60, 12
我想做的是为每个独特的campaign_objective 创建一列,并为其分配相应的支出值。
account_id, sales, brand, conversions
__________, _____, _____, ___________
1, 100, 50, 25
2, 80, 60, 12
请注意,首先,account_id 和转换如何保持不变,它们只是合并到每个 account_id 的一行中,但其次,每个广告系列目标都有自己的列(销售额和品牌),并且与 account_id 对应的支出值输入在适当的列下(分别是支出和品牌。)
如何在 Presto SQL 中完成此操作?
如果您事先知道所有潜在目标,则可以通过以下方式进行分组:
-- sample data
WITH dataset(account_id, campaign_objective, campaign_spend, conversions) AS (
VALUES (1, 'sales', 100,25),
(1, 'brand', 50,25),
(2, 'sales', 80,12),
(2, 'brand', 60,12)
)
-- query
select account_id
, sum(if(campaign_objective='brand', campaign_spend)) brand
, sum(if(campaign_objective='sales', campaign_spend)) sales
, max(conversions) conversions -- or arbitrary(conversions) or min(conversions)
from dataset
group by account_id
输出:
帐户ID | 牌 | 销售量 | 转换 |
---|---|---|---|
1 | 50 | 100 | 25 |
2 | 60 | 80 | 12 |
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.