2012-11-05 19 views
1

我想從列上的條件計數向SSRS提供5個計數。例如,假設色譜柱保持了產品的顏色 - 綠色,藍色,紅色和黃色。我想要做的是在單個查詢中返回每個數的計數。在SSRS數據集中爲單列返回4個單獨的計數

雖然我可以完成這讓使用case語句來完成:

Select 
     COUNT(*) 'Count', 
     case 
      When Color = 'BL' then 'Blue 
      When Color = 'RD' then 'Red 
      When Color = 'YL' then 'Yellow 
      When Color = 'GR' then 'Green 
      Else 'All Others' 
     End as Payment 
From COLORS(NoLock) 
Group by 
     case 
      When Color = 'BL' then 'Blue 
      When Color = 'RD' then 'Red 
      When Color = 'YL' then 'Yellow 
      When Color = 'GRthen ‘Green’ 
      Else 'All Others' 
     End 

當我使用的數據集是SSRS,我得到的是單數。我不想創建4個數據集查詢,因爲我實際上是通過參數開始和結束日期來選擇記錄,並且最終會得到5組日期參數。

+0

我編輯了自己的冠軍。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。另外,與論壇網站不同,我們不使用「謝謝」或「任何幫助表示讚賞」,或者在[so]上簽名。請參閱「[應該'嗨','謝謝',標語和致敬從帖子中刪除?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be -removed-from-posts) –

+0

我不確定這裏的問題似乎是什麼問題,我只是在SSRS中測試了這個查詢,並且它產生了5行數據,其中一個用於'顏色',然後是'其他所有其他'價值。你能澄清你正在嘗試做什麼嗎? – Taryn

回答

5

這應該做的伎倆

select count (*) as Total, 
    sum (case when color='BL' then 1 else 0 end) as BlueTotal, 
    sum (case when color='RD' then 1 else 0 end) as RedTotal, 
    sum (case when color='YL' then 1 else 0 end) as YellowTotal, 
    sum (case when color='GR' then 1 else 0 end) as GreenTotal 
from Colors 
+0

太棒了!謝謝 – Susan