{"id":188,"date":"2012-05-02T08:28:47","date_gmt":"2012-05-02T08:28:47","guid":{"rendered":"https:\/\/sqlserverlearner.azurewebsites.net\/2012\/05\/02\/group-by-month-year-with-example\/"},"modified":"2012-05-02T08:28:47","modified_gmt":"2012-05-02T08:28:47","slug":"group-by-month-year-with-example","status":"publish","type":"post","link":"https:\/\/sqlserverlearner.com\/2012\/05\/02\/group-by-month-year-with-example\/","title":{"rendered":"Group By Month, Year with example"},"content":{"rendered":"

In order to group by month you can use the SQL Server in built funtion Month().
\n[sql]
\nGROUP BY MONTH([datetimecolumn])
\n[\/sql]
\nTO group data by year you can use the in built funtion YEAR().
\n[sql]
\nGROUP BY YEAR([datetimecolumn])
\n[\/sql]
\nIn order to group by month and year you can use bith YEAR() and MONTH().
\n[sql]
\nGROUP BY YEAR([datetimecolumn])
\n[\/sql]<\/p>\n

Example code:
\n[sql]
\nCREATE TABLE #Sales
\n(
\n Name VARCHAR (100),
\n SalesDateTime DATETIME
\n)<\/p>\n

GO
\nINSERT INTO #Sales
\nSELECT ‘Product1’,
\n ‘2010-04-01 00:00:00.000’
\nUNION ALL
\nSELECT ‘Product2’,
\n ‘2010-04-02 00:00:00.000’
\nUNION ALL
\nSELECT ‘Product3’,
\n ‘2011-04-02 00:00:00.000’
\nUNION ALL
\nSELECT ‘Product4’,
\n ‘2011-04-03 00:00:00.000’
\nUNION ALL
\nSELECT ‘Product5’,
\n ‘2011-04-03 00:00:00.000’
\nUNION ALL
\nSELECT ‘Product1’,
\n ‘2012-04-30 00:00:00.000’
\nUNION ALL
\nSELECT ‘Product1’,
\n ‘2012-04-30 00:00:00.000’
\nUNION ALL
\nSELECT ‘Product1’,
\n ‘2012-05-02 00:00:00.000’
\nUNION ALL
\nSELECT ‘Product5’,
\n ‘2012-05-02 00:00:00.000’
\nUNION ALL
\nSELECT ‘Product5’,
\n ‘2012-05-02 00:00:00.000’<\/p>\n

GO
\n–GROUP BY Month
\nSELECT count(*) AS SalesCount, MONTH(SalesDateTime) Month
\nFROM #Sales
\nGROUP BY MONTH(SalesDateTime)<\/p>\n

–GROUP BY YEAR
\nSELECT count(*) AS SalesCount, YEAR(SalesDateTime) YEAR
\nFROM #Sales
\nGROUP BY YEAR(SalesDateTime)<\/p>\n

–GROUP BY Month and Year
\nSELECT count(*) AS SalesCount, YEAR(SalesDateTime) YEAR,MONTH(SalesDateTime) Month
\nFROM #Sales
\nGROUP BY YEAR(SalesDateTime),MONTH(SalesDateTime)<\/p>\n

GO
\nDROP TABLE #Sales
\n[\/sql]<\/p>\n

sql server 2005,2008,2008 r2,2012<\/p>\n","protected":false},"excerpt":{"rendered":"

In order to group by month you can use the SQL Server in built funtion Month(). [sql] GROUP BY MONTH([datetimecolumn]) [\/sql] TO group data by year you can use the in built funtion YEAR(). [sql] GROUP BY YEAR([datetimecolumn]) [\/sql] In order to group by month and year you can use bith YEAR() and MONTH(). [sql]…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41,1789],"tags":[1873,1874,1875,1876,1877,1878,1879,1880,1881,1882,1883,1884,1885,1886,1887,1888,1889],"_links":{"self":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts\/188"}],"collection":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/comments?post=188"}],"version-history":[{"count":0,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts\/188\/revisions"}],"wp:attachment":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/media?parent=188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/categories?post=188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/tags?post=188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}