How to group by month from Date field using sql

前端 未结 10 2070
自闭症患者
自闭症患者 2020-11-30 23:39

How can I group only by month from a date field (and not group by day)?

Here is what my date field looks like:

2012-05-01

Here is m

10条回答
  •  栀梦
    栀梦 (楼主)
    2020-12-01 00:14

    I used the FORMAT function to accomplish this:

    select
     FORMAT(Closing_Date, 'yyyy_MM') AS Closing_Month
     , count(*) cc 
    FROM
     MyTable
    WHERE
     Defect_Status1 IS NOT NULL
     AND Closing_Date >= '2011-12-01'
     AND Closing_Date < '2016-07-01' 
    GROUP BY FORMAT(Closing_Date, 'yyyy_MM')
    ORDER BY Closing_Month
    

提交回复
热议问题