简体   繁体   English

Redshift:我尝试使用 Union,但它返回 3 列而不是 4 列。我该怎么办?

[英]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.

相关问题 我可以使用什么来代替 orderbykey()? - What can I use instead of orderbykey()? 我可以继续使用 Aginity Workbench 进行 redshift 吗 - Can I continue to use Aginity Workbench for redshift 我可以使用 pm2 代替负载均衡器吗? - Can I use pm2 instead loadbalancer? 如何替换 Redshift 中的 JSON 值? - How can i replace JSON values in Redshift? 如何更改 Redshift 中的列数据类型? - How do I change column data type in Redshift? 我可以在阿里云中使用哪种资源来部署 Apache Beam 管道,在阿里云中我们是否有数据存储,如果没有,数据存储的替代方案是什么? - Which resource I can use in AliCloud to deploy Apache Beam pipeline, and in AliCloud Do we have datastore if not what is alternative for datastore? 我可以使用自定义 CloudWatch 指标做什么? - What can I use custom CloudWatch metrics for? 我可以在 Terraform 中创建一个 Redshift 集群并向其中添加一个额外的用户吗? - Can I create a Redshift cluster in Terraform AND add an additional user to it? 我可以通过 cloudformation 启用 Redshift 跨区域快照复制吗? - Can I enable Redshift cross region snapshot copy through cloudformation? 使用 aws redshift 在 dbt 中实现 scd2,如何定义条件自然键? - Implementing scd2 in dbt using aws redshift, how do I define conditional natural keys?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM