SQL Results group by month

后端 未结 2 1277
醉酒成梦
醉酒成梦 2021-02-08 04:12

I\'m trying to return some results spread over a rolling 12 month period eg:

MONTH       IN   OUT
January    210    191
February   200    111
March      132    1         


        
2条回答
  •  别那么骄傲
    2021-02-08 04:31

    You probably want something like

    SELECT SUM( (CASE WHEN problem_type = 'IN' THEN 1 ELSE 0 END) ) in,
           SUM( (CASE WHEN problem_type = 'OUT' THEN 1 ELSE 0 END) ) out,
           EXTRACT( year FROM DateTime ) year,
           EXTRACT( month FROM DateTime ) month
      FROM problem
     WHERE DateTime >= date '2010-01-01'
       AND DateTime < date '2010-01-31'
     GROUP BY EXTRACT( year FROM DateTime ),
              EXTRACT( month FROM DateTime )
    

提交回复
热议问题