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

SQL-Ergebnisse nach Monat gruppieren

Verwendung:

  SELECT SUM(CASE WHEN p.problem_type = 'IN' THEN 1 ELSE 0 END) AS IN, 
         SUM(CASE WHEN p.problem_type = 'OUT' THEN 1 ELSE 0 END) AS OUT, 
         TO_CHAR(datetime, 'YYYY') AS year,
         TO_CHAR(datetime, 'MM') AS month
    FROM PROBLEM p
   WHERE p.DateTime >= TO_DATE('2010-01-01', 'YYYY-MM-DD') 
     AND p.DateTime < TO_DATE('2010-01-31', 'YYYY-MM-DD') 
GROUP BY TO_CHAR(datetime, 'YYYY'), TO_CHAR(datetime, 'MM')

Sie könnten auch verwenden:

  SELECT SUM(CASE WHEN p.problem_type = 'IN' THEN 1 ELSE 0 END) AS IN, 
         SUM(CASE WHEN p.problem_type = 'OUT' THEN 1 ELSE 0 END) AS OUT, 
         TO_CHAR(datetime, 'MM-YYYY') AS mon_year
    FROM PROBLEM p
   WHERE p.DateTime >= TO_DATE('2010-01-01', 'YYYY-MM-DD') 
     AND p.DateTime < TO_DATE('2010-01-31', 'YYYY-MM-DD') 
GROUP BY TO_CHAR(datetime, 'MM-YYYY')

Referenz: