Oracle
 sql >> Datenbank >  >> RDS >> Oracle

Oracle SQL-Aggregatfunktion mit Standardwerten

Bearbeiten, um Nullen einzuschließen.

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