У таблицы tableA есть:
Я хочу получить таблицуI, как это (группа by startTime и endTime, количество Severity в столбце cnt и количество всех типов Severity в отдельном столбце):
Простой счетчик (столбец cnt) отлично работает. Но с другим я устал CASE КОГДА ТЕБЯ логики и, похоже, не работают (например, строка 10). Не могли бы вы помочь мне с SQL-запросом в этом случае.
Попробуйте выполнить запрос: с случаем, когда
select starttime, endtime, count(severity) as cnt, count(case when severity='LOW' then 1 end) cnt_low,count(case when severity='MED' then 1 end) cnt_med,count(case when severity='HIGH' then 1 end) as cnt_high
from tablename
group by starttime, endtime
use case when и aggregate function sum
select startTime , endTime,count(*) as Cnt,
sum( case when Severity='MED' then 1 else 0 end) as cntMed,
sum( case when Severity='LOW' then 1 else 0 end) as cntLow,
sum( case when Severity='HIGH' then 1 else 0 end) as cntHIGH from yourtable
group by startTime , endTime