我有桌子组,
ID NUMBER STATUS VARCHAR2(20 BYTE)
我能够算出以下状态数。
select g.status, count(*) from groups g group by g.status; STATUS COUNT(*) -------------------- ---------- OK 2 NOK 1
我还有另一种状态,例如“待审核,已拒绝”。但是表中没有项目,但是我希望它们显示为零计数,如下所示。
STATUS COUNT(*) -------------------- ---------- OK 2 NOK 1 PENDING 0 REJECTED 0
SQL语句将使之成为可能?
试试这个
SELECT A.status, COUNT(DISTINCT G.ID) statusCnt FROM (SELECT 'OK' status FROM DUAL UNION SELECT 'NOK' status FROM DUAL UNION SELECT 'PENDING' status FROM DUAL UNION SELECT 'REJECTED' status FROM DUAL ) AS A LEFT JOIN groups G ON A.status = G.STATUS GROUP BY A.status;