Well... I have a bit of a problem with your approach. I mean... under the right circumstances, it might be fine, but it still scares me a little. Let me explain...
Each year has it's own set of weeks. The first day of the week may change depending on year. For example, Jan 3 may be for the first day for year 2011, but the first day of the first week is likely going to be different for 2012 and 2010. This is why I don't like using the week parameter for the datepart function.
so I'm selecting the week number then grouping on it.
If your data spans more than a year, then your DatePart(Week,) calculation will group data from multiple years in to one. This may be what you want, but it might not either.
Instead, I would calculate the first day of each week for your createdate column, and then use that to group on (effectively killing 2 birds with one stone). This method will also accommodate multiple years.
I encourage you to take a look at this code:
Code:
Declare @Temp Table(CreateDate DateTime)
insert into @temp Values('2011-02-25')
insert into @temp Values('2011-02-26')
insert into @temp Values('2011-02-27')
insert into @temp Values('2011-02-28')
insert into @temp Values('2011-03-01')
insert into @temp Values('2011-03-02')
insert into @temp Values('2011-03-03')
insert into @temp Values('2011-03-04')
insert into @temp Values('2011-03-05')
insert into @temp Values('2011-03-06')
insert into @temp Values('2011-03-07')
Select CreateDate, DateAdd(Day, -DateDiff(Day, [!]-1[/!], CreateDate) %7, CreateDate)
From @Temp
Note the -1. If I interpret your question correctly, you want the week to start on Sunday. Day # -1 is actually December 31, 1899, which happens to be a Sunday. If you want your first day of the week to be Monday (or any other day), then change the -1 value.
Now... if you use this calculation to do your grouping, you'll already have the first day of the week AND your grouping will respect year boundaries.
what I want is to sum up some data by week
Let's see my method in action:
Code:
Declare @Temp Table(CreateDate DateTime, PizzasEaten Int)
insert into @temp Values('2011-02-25', 2)
insert into @temp Values('2011-02-26', 1)
insert into @temp Values('2011-02-27', 0)
insert into @temp Values('2011-02-28', 3)
insert into @temp Values('2011-03-01', 5)
insert into @temp Values('2011-03-02', 8)
insert into @temp Values('2011-03-03', 4)
insert into @temp Values('2011-03-04', 10)
insert into @temp Values('2011-03-05', 1)
insert into @temp Values('2011-03-06', 4)
insert into @temp Values('2011-03-07', 6)
Select DateAdd(Day, -DateDiff(Day, -1, CreateDate) %7, CreateDate) As StartOfWeek,
Sum(PizzasEaten) As PizzaCount
From @Temp
Group BY DateAdd(Day, -DateDiff(Day, -1, CreateDate) %7, CreateDate)
As you can see, the data is broken out by week with the first day of the week shown in the output.
** Note: I really don't eat that many pizzas.
Make sense?
-George
Microsoft SQL Server MVP
My Blogs
SQLCop
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom