簡體   English   中英

Mysql按3個字段分組

[英]Mysql Group by 3 fields with count

請參閱所附圖片以查看我的數據。

我試圖得到這樣的結果:

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

我試圖計算每個SessionNumber和EventDate的各種primaryAssembly,主題,注冊。

這是我正在使用的查詢:

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

但是我寧願不必使用“ Where”子句。 我想對術語本身進行分組。 這是一個屏幕截圖: 在此處輸入圖片說明

數據透視查詢可以幫助:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM