[英]Redshift: I try to use Union but it returns 3 columns instead of 4. What can I do?
I have to find the streams that took place in a specific country and specific dates (overall_streams) and then for the same country and dates, I have to find the streams for a specific product.我必须找到在特定国家和特定日期 (overall_streams) 发生的流,然后对于相同的国家和日期,我必须找到特定产品的流。
In other words, I am trying to compare how the product did compared to the overall number of streams that took place in this place and time.换句话说,我试图将产品的表现与在此时间和地点发生的流的总数进行比较。
For this reason, I tried to use UNION (the subquery I did wouldn't give the right results).出于这个原因,我尝试使用 UNION(我做的子查询不会给出正确的结果)。
Here is my- simplified- code:这是我的简化代码:
Select age_group, gender, sum(streams) as product_streams
From t1
Where product='A'
And country= 'US'
And date= '1st week of July'
Group by 1,2
Union
Select age_group, gender, sum(streams) as overall_streams
From t1
Where country='US'
And date='1st week of July'
Group by 1,2
Notice the difference in the second query is that I haven't specified a product.请注意第二个查询的不同之处在于我没有指定产品。
The results I get is 3 columns.我得到的结果是 3 列。 The third column is named "product_streams" and it alternates between the product_streams and the overall_streams.
第三列名为“product_streams”,它在 product_streams 和 overall_streams 之间交替。
Example:例子:
0-18 f 100
0-18 f 560
0-18 m 45
0-18 m 398
The results are correct, I just want to have 4 columns instead of 3. Like this:结果是正确的,我只想有 4 列而不是 3 列。像这样:
age_group gender product_streams overall_streams
Any ideas?有任何想法吗?
I think you want conditional aggregation:我想你想要条件聚合:
Select age_group, gender,
sum(streams) as overall_streams
sum(case when product = 'A' then streams else 0 end) as product_streams
From t1
Where country = 'US' and
date = '1st week of July'
group by age_group, gender;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.