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

Risultati SQL raggruppati per mese

Usa:

  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')

Potresti anche usare:

  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')

Riferimento: