2015-06-22 62 views
2

這是我用來計算每個預訂類別預訂了多少個事件的查詢。SQL Group按多個類別

SELECT 
    Categories.name, 
    count(case when locations.name ='loc 1' then 1 end) as Location1, 
    count(case when locations.name ='loc 2' then 1 end) as Location2, 
    count(case when locations.name ='loc 3' then 1 end) as Location3, 
    count(Categories.name) as total 

FROM 
    ... 

group by Categories.name 
with rollup 

這給了我以下結果:

+------------------------------------------------------------+ 
|Category_name | Location1 | Location2 | Location3 | Total | 
+------------------------------------------------------------+ 
|Cat1   | 1   | 2   | 2   | 5  | 
|Cat2   | 2   | 1   | 2   | 5  | 
|Cat3   | 2   | 2   | 2   | 6  | 
|Cat3(Extra)  | 1   | 3   | 2   | 6  | 
|Cat4   | 3   | 1   | 2   | 6  | 
+------------------------------------------------------------+ 
|Total per loc | 9   | 9   | 10  | 28 | 
+------------------------------------------------------------+ 

這是令人滿意的到現在爲止! 現在我需要做同樣的請求,但我想我需要通過(??)更改組。 有沒有辦法改變我的要求是:

Count for Cat1 the number of events per location. 
Count for Cat2 the number of events per location. 
Count for Cat3 and Cat3(extra) the number of events per location. 
Count for Cat4 the number of events per location. 

我的意思是某些類別需要算在一起,什麼我期待實現的是這樣的結果:

+------------------------------------------------------------+ 
|Category_name | Location1 | Location2 | Location3 | Total | 
+------------------------------------------------------------+ 
|Cat1   | 1   | 2   | 2   | 5  | 
|Cat2   | 2   | 1   | 2   | 5  | 
|Cat3   | 3   | 5   | 4   | 12 | 
|Cat4   | 3   | 1   | 2   | 6  | 
+------------------------------------------------------------+ 
|Total per loc | 9   | 9   | 10  | 28 | 
+------------------------------------------------------------+ 
莫非

你請幫我到那裏。謝謝。 SB

+0

不是最優的,但是一種方法是按類別名稱的前4個字符進行分組,而不是完整的名稱。像這樣:http://stackoverflow.com/questions/666525/group-by-first-character – Maximus2012

+0

謝謝。我更喜歡使用一個語句(CASE當Categories.name像'Cat3%'THEN'Cat3'ELSE Categories.name END)以避免類別名稱過於相似時可能出現的錯誤。非常感謝您的意見。 –

+0

當然。另一種方法(我認爲可能有更好的性能和功能)可以爲category_group創建一個整數列,將Cat3和Cat3(Extra)分配給相同的category_group,然後執行GROUP BY category_group。不知道在你的情況下,你允許多少表格修改。 – Maximus2012

回答

2

就包括了group by表達case聲明:

SELECT (CASE WHEN Categories.name like 'Cat3%' THEN 'Cat3' 
       ELSE Categories.name 
      END) as name, 
     sum(locations.name = 'loc 1') as Location1, 
     sum(locations.name = 'loc 2') as Location2, 
     sum(locations.name = 'loc 3') as Location3, 
     count(*) as total 
    FROM ... 
    GROUP BY (CASE WHEN Categories.name like 'Cat3%' THEN 'Cat3' 
       ELSE Categories.name 
      END) 
+0

謝謝戈登。我添加了一個涵蓋我所需要的所有條件的案例陳述。再次感謝! –

0

肯定,只是添加更改組定義的新名稱合併成一個單一的「組」 ......

SELECT 
    substring(Categories.name, 1, 4) CategoryName, 
    count(case when locations.name ='loc 1' then 1 end) Location1, 
    count(case when locations.name ='loc 2' then 1 end) Location2, 
    count(case when locations.name ='loc 3' then 1 end) Location3, 
    count(Categories.name) total, 

FROM 
    ... 

group by substring(Categories.name, 1, 4) 
with rollup 
+0

謝謝查爾斯。我更喜歡使用一個語句(CASE當Categories.name像'Cat3%'THEN'Cat3'ELSE Categories.name END)以避免類別名稱過於相似時可能出現的錯誤。非常感謝您的意見。 –