sudhir
sudhir

Reputation: 219

AWS Redshift sql grouping by

I am working on Redshift database table which has below columns.

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". 1 - Company impacted, 2 - Org impacted should be grouped as Severity "High".

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.

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.

Upvotes: 1

Views: 606

Answers (1)

zealous
zealous

Reputation: 7503

You have to add case expressions in group by as following

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;

Upvotes: 1

Related Questions