I know I’m going to go ‘duh’ when I see the answer to this, but nonetheless:
How do I group a date field by week, where the weeks start on Saturday?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You need to first use DATEFIRST to set
Saturdayas the first day of your week. Value 1 denotes Monday and value 6 denotes the day Saturday. Then you can use the DATEPART withweekorwkorwwto group your date column values.You can see in the below sample data that values for dates
2012-01-07and2012-01-08are grouped under weekno 2 because 2012-01-07 is a Saturday. January 1 of any year is always first week of the year.Click here to view the output in SQL Fiddle.
Script:
Output: