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

SQL - Alternative zu WITH ... AS

Verwenden Sie einfach die bedingte Aggregation:

SELECT SUM(CASE WHEN STATUS = 'A' THEN 1 ELSE 0 END) AS A_COUNT ,
       SUM(CASE WHEN STATUS = 'B' THEN 1 ELSE 0 END) AS B_COUNT ,
       SUM(CASE WHEN STATUS = 'C' THEN 1 ELSE 0 END) AS C_COUNT ,
       SUM(CASE WHEN STATUS = 'D' THEN 1 ELSE 0 END) AS D_COUNT ,
       SUM(CASE WHEN STATUS = 'E' THEN 1 ELSE 0 END) AS E_COUNT ,
       SUM(CASE WHEN STATUS = 'F' THEN 1 ELSE 0 END) AS F_COUNT 
FROM XYZ ;

Ich sollte darauf hinweisen, dass es noch einfacher ist, die Werte in separate Zeilen zu schreiben:

select status, count(*)
from xyz
group by status;