{"id":187,"date":"2012-05-02T08:10:27","date_gmt":"2012-05-02T08:10:27","guid":{"rendered":"https:\/\/sqlserverlearner.azurewebsites.net\/2012\/05\/02\/group-by-day-with-examples\/"},"modified":"2012-05-02T08:10:27","modified_gmt":"2012-05-02T08:10:27","slug":"group-by-day-with-examples","status":"publish","type":"post","link":"https:\/\/sqlserverlearner.com\/2012\/05\/02\/group-by-day-with-examples\/","title":{"rendered":"Group by Day – With Examples"},"content":{"rendered":"

How to Group by Day in sql server?<\/strong><\/p>\n

Grouping By Day Of month:
\nIn order to group the data by day you can use sql server in built DAY() funtion.<\/p>\n

Grouping By Day Of week:
\nFor this you can use DATEPART(weekday,[date column])<\/p>\n

Grouping By week:
\nFor this you can use DATEPART(week,[date column])<\/p>\n

Grouping By DAY of Year:
\nFor this you can use DATEPART(dayofyear,[date column])<\/p>\n

Let us suppose the column which contains date\/datetime values is solddatetime.<\/p>\n

To group by day of month you can use:
\n[sql]
\nGROUP BY DAY(SalesDateTime)
\n[\/sql]<\/p>\n

To group by Day of week you can use:
\n[sql]
\nGROUP BY DATEPART(weekday,SalesDateTime)
\n[\/sql]<\/p>\n

To group by week you can use:
\n[sql]
\nGROUP BY DATEPART(week,SalesDateTime)
\n[\/sql]<\/p>\n

To group by Day of year you can use:
\n[sql]
\nGROUP BY DATEPART(dayofyear,SalesDateTime)
\n[\/sql]<\/p>\n

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

GO
\nINSERT INTO #Sales
\nSELECT ‘Product1’,
\n ‘2012-04-01 00:00:00.000’
\nUNION ALL
\nSELECT ‘Product2’,
\n ‘2012-04-02 00:00:00.000’
\nUNION ALL
\nSELECT ‘Product3’,
\n ‘2012-04-02 00:00:00.000’
\nUNION ALL
\nSELECT ‘Product4’,
\n ‘2012-04-03 00:00:00.000’
\nUNION ALL
\nSELECT ‘Product5’,
\n ‘2012-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 DAY of Month
\nSELECT count(*) AS SalesCount, DAY(SalesDateTime) Day
\nFROM #Sales
\nGROUP BY DAY(SalesDateTime)<\/p>\n

–GROUP BY DAY of Week
\nSELECT count(*) AS SalesCount, DATEPART(weekday,SalesDateTime) Day
\nFROM #Sales
\nGROUP BY DATEPART(weekday,SalesDateTime)<\/p>\n

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

–GROUP BY DAY of Year
\nSELECT count(*) AS SalesCount, DATEPART(dayofyear,SalesDateTime) Day
\nFROM #Sales
\nGROUP BY DATEPART(dayofyear,SalesDateTime)<\/p>\n

GO
\nDROP TABLE #Sales
\n[\/sql]<\/p>\n","protected":false},"excerpt":{"rendered":"

How to Group by Day in sql server? Grouping By Day Of month: In order to group the data by day you can use sql server in built DAY() funtion. Grouping By Day Of week: For this you can use DATEPART(weekday,[date column]) Grouping By week: For this you can use DATEPART(week,[date column]) Grouping By DAY…<\/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":[1863,1864,1865,1866,1867,1868,1869,1870,1871,1872],"_links":{"self":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts\/187"}],"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=187"}],"version-history":[{"count":0,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts\/187\/revisions"}],"wp:attachment":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/media?parent=187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/categories?post=187"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/tags?post=187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}