2015-11-18 52 views
0

我有我試圖理解和轉換 到一個DB2格式以下查詢:從Access轉換聲明,以DB2

TRANSFORM Sum(Cases) AS SumOfCases 
SELECT Process, Sum(Cases) AS total 
FROM tbl 
GROUP BY Process 
PIVOT tbl.STATUS; 

表有類似的數據:

進程狀態例
一個取消14
封閉179
b取消20
B關閉30
b等待10

我怎麼可以編寫查詢到DB2? 我嘗試以下查詢:

SELECT Process 
, MAX(CASE WHEN STATUS = 'Cancelled' THEN CASES END) "Cancelled" 
, MAX(CASE WHEN STATUS = 'Closed' THEN CASES END) "Closed" 
, MAX(CASE WHEN STATUS = 'Pending' THEN CASES END) "Pending" 
FROM tbl 
GROUP BY Process; 

因爲我沒有的MS Access因此我沒有信心,如果我做了正確的事DB2與否。如果我能得到這方面的一些建議

將不勝感激。

回答

1

您的DB2查詢在複製Access的交叉表查詢時正常工作,除非您錯過了總計列。順便說任何aggregate function會爲您CASE/WHEN語句的工作:MIN()MAX()MEDIAN()AVG(),甚至SUM()

SELECT Process 
, SUM(CASES) AS "Total" 
, MAX(CASE WHEN STATUS = 'Cancelled' THEN CASES END) AS "Cancelled" 
, MAX(CASE WHEN STATUS = 'Closed' THEN CASES END) AS "Closed" 
, MAX(CASE WHEN STATUS = 'Pending' THEN CASES END) AS "Pending" 
FROM tbl 
GROUP BY Process; 
+0

感謝芭菲,我想有專家:)第2個意見。 哦,我沒有注意到我忘記了聚合函數。 –