enkiki
enkiki

Reputation: 399

MySQL CASE with COUNT query and adding another column

I'm trying to add a column (CLIENT) to my following MySQL query:

My table

CLIENT  SEVERITYLEVEL TOTAL
  Bob        4        27
  Bob        3        24
  Bob        7        19
  Bob        5        10
  Bob        8        9
  Bob        6        7
  Bob        1        5
  Bob        2        3
  Gin        2        6
  Gin        3        7

My Desired Output

CLIENT SEVERITYLEVEL  Total

Bob    Severe         63
Bob    Moderate       32
Bob    Critical       9

...

select 
(case when (`s2`.`SEVERITYLEVEL` = 1) then 'Moderate' 
 when (`s2`.`SEVERITYLEVEL` = 2) then 'Moderate' 
 when (`s2`.`SEVERITYLEVEL` = 3) then 'Moderate' 
 when (`s2`.`SEVERITYLEVEL` = 4) then 'Severe' 
 when (`s2`.`SEVERITYLEVEL` = 5) then 'Severe' 
 when (`s2`.`SEVERITYLEVEL` = 6) then 'Severe' 
 when (`s2`.`SEVERITYLEVEL` = 7) then 'Severe' 
 when (`s2`.`SEVERITYLEVEL` = 8) then 'Critical' 
 when (`s2`.`SEVERITYLEVEL` = 9) then 'Critical' 
 when (`s2`.`SEVERITYLEVEL` = 10) then 'Critical' 
 when (`s2`.`SEVERITYLEVEL` = 10) then 'Critical' end) 
 AS `SEVERITYLEVEL`,count(0) AS `total` 

 from `s2` 
 group by (case when (`s2`.`SEVERITYLEVEL` = 1) then 'Moderate' 
 when (`s2`.`SEVERITYLEVEL` = 2) then 'Moderate' 
 when (`s2`.`SEVERITYLEVEL` = 3) then 'Moderate' 
 when (`s2`.`SEVERITYLEVEL` = 4) then 'Severe' 
 when (`s2`.`SEVERITYLEVEL` = 5) then 'Severe' 
 when (`s2`.`SEVERITYLEVEL` = 6) then 'Severe' 
 when (`s2`.`SEVERITYLEVEL` = 7) then 'Severe' 
 when (`s2`.`SEVERITYLEVEL` = 8) then 'Critical' 
 when (`s2`.`SEVERITYLEVEL` = 9) then 'Critical' 
 when (`s2`.`SEVERITYLEVEL` = 10) then 'Critical' end) 
 order by count(0) desc limit 10

I got the table working correctly for the 2nd/3rd column (SEVERITY LEVEL+TOTAL) but how can I add CLIENT as well ?

Upvotes: 0

Views: 118

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1269633

You need to add the name. The query can be simplified to:

select name,
      (case when s2.SEVERITYLEVEL in (1, 2, 3) then 'Moderate' 
            when s2.SEVERITYLEVEL in (4, 5, 6, 7) then 'Severe' 
            when s2.SEVERITYLEVEL in (8, 9, 10) then 'Severe' 
       end) as severity,
      count(*) as total 
from s2
group by name, severity
order by count(*) desc
limit 10;

Notes:

  • MySQL lets you use aliases in the group by. This is a big convenience.
  • Give the new column name a new name. This is just generally good advice to keep things unambiguous.
  • Superfluous backticks just make the query harder to write and to read. No harm, but why bother?
  • I understand count(1) but count(0) just seems confusing, especially for less experienced people. count(*) is the SQL standard construct for counting rows.

Upvotes: 1

Alex
Alex

Reputation: 17289

I would strongly recommend to put your severities into separate table. That would help to avoid a lot of complexity in future and make many queries much more performant.

http://sqlfiddle.com/#!9/9f25cf/3

SELECT client, title, sum(total)
FROM s2 
LEFT JOIN severity
ON s2.severitylevel = severity.id
GROUP BY client, title

Upvotes: 1

Related Questions