简体   繁体   English

AWS Redshift sql 分组方式

[英]AWS Redshift sql grouping by

I am working on Redshift database table which has below columns.我正在研究具有以下列的 Redshift 数据库表。

Impact                                                rootcause
1 - Company impacted                                  dependent
2 - Org impacted                                      dependent
2 - Org impacted                                      Monitoring Autocut
2 - Org impacted                                      Duplicate
3 - Group impacted                                    Operational - Question/No Issue
3 - Group impacted                                    Monitoring Autocut
3 - Group impacted                                    Duplicate
4 - Individual impacted                               Operational - Question/No Issue
4 - Individual impacted                               Monitoring Autocut
4 - Individual impacted                               Duplicate
5 - No impact                                         Operational - Question/No Issue
5 - No impact                                         Monitoring Autocut
5 - No impact                                         Duplicate

My goal is to get data in below format.我的目标是获取以下格式的数据。 3 - Group impacted, 4 - Individual impacted, 5 - No impact should be grouped as Severity "Low". 3 - 群体影响,4 - 个人影响,5 - 无影响应归为“低”严重性。 1 - Company impacted, 2 - Org impacted should be grouped as Severity "High". 1 - 公司受影响,2 - 受影响的组织应归为“高”严重性。

Severity              rootcause                                   count
Low                   Operational - Question/No Issue             3
Low                   Monitoring Autocut                          3
Low                   Duplicate                                   3
High                  dependent                                   2
High                  Monitoring Autocut                          1
High                  Duplicate                                   1

I have written below sql query.我在下面写了 sql 查询。

select 
CASE WHEN Impact IN ('1 - Company impacted','2 - Org impacted') THEN 'High' 
WHEN assigned_min_impact IN ('3 - Group impacted','4 - Individual impacted','5 - No impact') THEN 'Low' 
ELSE 'NA' END as severity, 
rootcause, count(*) from TABLE 
group by Impact, root_cause;

Above query is not giving desired result.上面的查询没有给出想要的结果。 Can someone please me help with this.有人可以请我帮忙吗?

You have to add case expressions in group by as following您必须在group by中添加case表达式,如下所示

select 
  CASE 
    WHEN Impact IN ('1 - Company impacted','2 - Org impacted') THEN 'High' 
    WHEN assigned_min_impact IN ('3 - Group impacted','4 - Individual impacted','5 - No impact') THEN 'Low' 
    ELSE 'NA' 
  END as severity, 
  rootcause, 
  count(*) 
from TABLE 
group by 
  rootcause,   
  CASE 
    WHEN Impact IN ('1 - Company impacted','2 - Org impacted') THEN 'High' 
    WHEN assigned_min_impact IN ('3 - Group impacted','4 - Individual impacted','5 - No impact') THEN 'Low' 
    ELSE 'NA' 
  END;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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