Jeff Moden wrote:wrote:SELECT DATEADD(DAY, -DATEDIFF(DAY, 0, [Order Date]) % 7, [Order Date]) AS [Week of],
COUNT(*) AS Count
FROM dbo.your_table_name
GROUP BY DATEADD(DAY, -DATEDIFF(DAY, 0, [Order Date]) % 7, [Order Date])
Welcome aboard! That's a great bit of code considering the OP offered almost no details.
It does seem to assume that weeks start on Mondays (which I totally agree with and is ISO). We'll see how the OP defines what a week is.
Not so sure about that. I live in the middle east and here the week starts at Sunday. I think that this would also be the case at most Muslim countries since for Muslims Friday is the holy day so the weekend starts at Friday and not at Saturday. In any case I agree with you that the original poster should have write more details.
Adi
A lot of companies here in the US do the same thing (week starts on Sunday). Even SQL Server defaults to Sunday start dates. I'm also familiar with the Muslim calendar you mention an other calendars such as the first day of the week being Thursday.
Just to be sure, none of the reasons I have for agreeing with Monday start dates have a thing to do with religion. Like I said, one of the reasons is that it's ISO compliant. The other reasons are mostly for computational convenience... 01 Jan 1900 and 01 Jan 1753 both occurred on Mondays.
Heh... I also like the fact that "the weekend" consists of two days that start with the same letter as some of my goals for weekends... simple, safe, stress-free, salubrious, satisfying, solitude. 😀
--Jeff Moden
Change is inevitable... Change for the better is not.