1

我想将这些选择的结果放在一个视图中。公共列是 RUNID,但是右表的结果并不总是存在。所以它应该返回空。我认为所有这些选择都可以组合成一个选择,将 RUNID 传递给不同标准的每个计数。

--COUNT OF S16 ELIGIBLE ACCOUNTS
SELECT    PDDETAIL.RUNID, COUNT(*) AS "S16 ELIGIBLE ACCOUNTS"
FROM      PDDETAIL
LEFT JOIN PDSTAGES ON (PDDETAIL.PDID = PDSTAGES.PDID AND PDDETAIL.CUST_ID = PDSTAGES.CUST_ID)
WHERE     PDSTAGES.PDSTAGE = 'S16ELIGIBLE'
GROUP BY  PDDETAIL.RUNID
ORDER BY  PDDETAIL.RUNID ASC
;

--COUNT OF S16 MTO SUBMITTED
SELECT    PDDETAIL.RUNID,COUNT(*) AS "S16 MTO SUBMITTED"
FROM      PDDETAIL
LEFT JOIN PDSTAGES ON (PDDETAIL.PDID = PDSTAGES.PDID AND PDDETAIL.CUST_ID = PDSTAGES.CUST_ID)
WHERE     PDSTAGES.PDSTAGE = 'S16MTOREFRESHSUBMITTED'
GROUP BY  PDDETAIL.RUNID
ORDER BY  PDDETAIL.RUNID ASC
;

--COUNT OF S16 MTO RETURN FAILED
SELECT    PDDETAIL.RUNID,COUNT(*) AS "S16 MTO FAILED"
FROM      PDDETAIL
LEFT JOIN PDSTAGES ON (PDDETAIL.PDID = PDSTAGES.PDID AND PDDETAIL.CUST_ID = PDSTAGES.CUST_ID)
WHERE     PDSTAGES.PDSTAGE = 'S16MTORETURNFAILED'
GROUP BY  PDDETAIL.RUNID
ORDER BY  PDDETAIL.RUNID ASC
;
4

1 回答 1

3
SELECT    
 PDDETAIL.RUNID,
 COUNT(CASE WHEN PDSTAGES.PDSTAGE = 'S16ELIGIBLE' THEN 1 END) AS "S16 ELIGIBLE ACCOUNTS",
 COUNT(CASE WHEN PDSTAGES.PDSTAGE = 'S16MTOREFRESHSUBMITTED' THEN 1 END) AS "S16 MTO SUBMITTED",
 COUNT(CASE WHEN PDSTAGES.PDSTAGE = 'S16MTORETURNFAILED' THEN 1 END) AS "S16 MTO FAILED"
FROM      PDDETAIL
LEFT JOIN PDSTAGES ON (PDDETAIL.PDID = PDSTAGES.PDID AND PDDETAIL.CUST_ID = PDSTAGES.CUST_ID)
GROUP BY  PDDETAIL.RUNID
ORDER BY  PDDETAIL.RUNID ASC;
于 2012-09-24T15:49:45.897 回答