Oracle
 sql >> Database >  >> RDS >> Oracle

Oracle sql funzione di aggregazione con valori predefiniti

Modifica per includere gli 0.

SELECT
  id_list.id,
  status_list.status,
  COUNT(counterTb.id)
FROM
(
  SELECT 3 AS id FROM dual
)
  id_list
CROSS JOIN
(
  SELECT 'YES' AS status FROM dual
  UNION ALL
  SELECT 'NO'  AS status FROM dual
)
  status_list
LEFT JOIN
  counterTb
    ON  counterTb.id     = id_list.id
    AND counterTb.status = status_list.status
GROUP BY
  id_list.id,
  status_list.status
ORDER BY
  id_list.id,
  status_list.status DESC