2012-04-20 87 views
1

我需要有人來糾正下面的陳述。先謝謝你。我如何得到2個不同列的相同總值

SELECT CATEGORY 
--WHAT PERIOD? 
,'P3' AS PERIOD 
,'2013' AS FISCALYEAR   
,COUNT(CASE SecurityLayer WHEN 'dblayer' THEN SecurityLayer ELSE '' END) DB_SEC_COUNT   
,COUNT(CASE SecurityLayer WHEN 'Applayer' THEN SecurityLayer ELSE '' END) APP_SEC_COUNT 
FROM [db_eCAM].[dbo].[tbl_SecChecks] 
GROUP BY CATEGORY 
+1

什麼是表的列,該錯誤是什麼? – hkutluay 2012-04-20 19:49:10

+0

我正在做一個聚合。 – Yves 2012-04-20 19:51:13

回答

2

您是否試圖返回每個匹配項的次數?然後用

SELECT CATEGORY 
--WHAT PERIOD? 
,'P3' AS PERIOD 
,'2013' AS FISCALYEAR 
,SUM(CASE SecurityLayer WHEN 'dblayer' then 1 else 0 end) AS DB_SEC_COUNT 
,SUM(CASE SecurityLayer WHEN 'Applayer' then 1 else 0 end) AS APP_SEC_COUNT 
FROM [db_eCAM].[dbo].[tbl_SecChecks] 
GROUP BY CATEGORY 

試試看。

+0

問題解決了!非常感謝 (: – Yves 2012-04-20 19:53:52

2

而是計算列的嘗試總結它(否則每一行仍將無論價值計):

SELECT CATEGORY 
--WHAT PERIOD? 
,'P3' AS PERIOD 
,'2013' AS FISCALYEAR   
,SUM(CASE SecurityLayer WHEN 'dblayer' THEN 1 ELSE 0 END) DB_SEC_COUNT   
,SUM(CASE SecurityLayer WHEN 'Applayer' THEN 1 ELSE 0 END) APP_SEC_COUNT 
FROM [db_eCAM].[dbo].[tbl_SecChecks] 
GROUP BY CATEGORY 
相關問題