从 Access 到 DB2 的转换语句

Transform Statement from Access to DB2

I have the following query that I am trying to understand and convert it to a db2 format:

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

Table has data like:

Process Status Cases
a Cancelled 14
a Closed 179
b Cancelled 20
b Closed 30
b Pending 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;

Since I do not have MS Access hence I am not confident that if I had done the right thing in db2 or not.

Would appreciate if I could get some advice on this.

您的 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;