Mysql Group по 3 полям с количеством - PullRequest
0 голосов
/ 17 мая 2018

См. Прикрепленное изображение, чтобы увидеть данные, которые у меня есть.

Я пытаюсь получить результат, подобный следующему:

SessionNumber | Event Date| Critical Care Count | Pulmonary Circulation
G1            | 5/19/2018 | 2                   | 3
G1            | 5/20/2018 | 5                   | 1
PCC1          | 5/19/2018 | 4                   | 5

Я пытаюсь сосчитать различные первичные сборки, тема, reg для SessionNumber и EventDate.

Этот запрос я использую:

select SessionNumber, EventDate, count(distinct BadgeID) as CriticalCareCount 
from beacon 
where primaryAssembly="Critical Care" 
group by SessionNumber, EventDate 
order by EventDate;

Но я бы предпочел не использовать предложение «Где».Я хотел бы группировать по самому термину.Вот снимок экрана: enter image description here

1 Ответ

0 голосов
/ 17 мая 2018

Может помочь сводный запрос:

SELECT SessionNumber,Event_Date,
       count( case when primaryAssembly = 'Critical Care' then 1 end ) 
                   As Critical_Care_Count,
       count( case when primaryAssembly = 'Pulmonary Circulation' then 1 end ) 
                   As Pulmonary_Circulation_Count,
       count( case when primaryAssembly = 'Some other value' then 1 end ) 
                   As Some_other_value_Count,
       ......
       ......
       count( case when some_other_logical_condition then 1 end ) 
                   As some_other_condition_count
       ......
       ......
       SUM( case when primaryAssembly = 'Critical Care' then Duration else 0 end ) 
                   As sum_of_duration_for_critical_care
       ......
       ......
       count(*) As total_count
FROM table
GROUP BY SessionNumber,Event_Date
...