Oracle
 sql >> Base de données >  >> RDS >> Oracle

Groupe de résultats SQL par mois

Utiliser :

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

Vous pouvez également utiliser :

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

Référence :