具有CASE WHEN的MySQL具有多个值

时间:2019-02-19 12:04:09

标签: mysql

我正在尝试显示严重程度的总和。

我的桌子

client    dateadded     problem        level
 abc      2019-02-02    12345a         1
 abc      2019-02-02    12345b         1
 abc      2019-02-02    12345c         2
 abc      2019-02-02    12345d         5
 abc      2019-02-09    12345e         3
 abc      2019-02-09    12345f         3
 abc      2019-02-09    12345g         4
 abc      2019-02-09    12345h         10
 abc      2019-02-09    12345j         8
 abc      2019-02-16    12345x         7
 abc      2019-02-16    12345s         9
 abc      2019-02-16    12345w         4
 abc      2019-02-16    12345bs        5

这是我的代码

select client, dateadded, 
count(case when level= '1,2,3' then 1 end) as Moderate,
count(case when level= '4,5,6,7' then 1 end) as Severe,
count(case when level= '8,9,10' then 1 end) as Critical          
from table1 where client = 'abc'
group by client, dateadded

我尝试过

count(case when level= '1' and '2' and '3' then 1 end) as Moderate,   

我想要的输出

dateadded      Moderate    severe     critical
2019-02-02        3           1           0
2019-02-09        2           1           2
2019-02-16        0           3           2

谢谢! 娜塔莉(Nathalie)

2 个答案:

答案 0 :(得分:0)

您可以在IN中使用case条件并按dateadded分组吗

select client, dateadded, 
count(case when level IN (1,2,3) then 1 end) as Moderate,
count(case when level IN (4,5,6,7) then 1 end) as Severe,
count(case when level IN (8,9,10) then 1 end) as Critical          
from table1 where client = 'abc'
group by dateadded, client

答案 1 :(得分:0)

当IN(1,2,3)THEN 1 END ...成为例时....谢谢大家!