holemania
asked on
SQL Query - Grouping by weekly
Hello experts,
I need to group some data base of dates. It needs to be grouped base on a weekly date. See example.
Raw data:
ID HOURS DATE
8922 10.00 8/30/2010
8923 09.00 8/31/2010
8924 08.00 9/2/2010
8930 09.00 9/2/2010
8941 04.00 9/6/2010
8942 09.00 9/8/2010
8945 03.00 9/10/2010
Need to group it like the following:
ID HOURS DATE
8/30 - 9/3
8922 10.00 8/30/2010
8923 09.00 8/31/2010
8924 08.00 9/2/2010
8930 09.00 9/2/2010
9/6 - 9/10
8941 04.00 9/6/2010
8942 09.00 9/8/2010
8945 03.00 9/10/2010
I need to group some data base of dates. It needs to be grouped base on a weekly date. See example.
Raw data:
ID HOURS DATE
8922 10.00 8/30/2010
8923 09.00 8/31/2010
8924 08.00 9/2/2010
8930 09.00 9/2/2010
8941 04.00 9/6/2010
8942 09.00 9/8/2010
8945 03.00 9/10/2010
Need to group it like the following:
ID HOURS DATE
8/30 - 9/3
8922 10.00 8/30/2010
8923 09.00 8/31/2010
8924 08.00 9/2/2010
8930 09.00 9/2/2010
9/6 - 9/10
8941 04.00 9/6/2010
8942 09.00 9/8/2010
8945 03.00 9/10/2010
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Awesome. This is exactly what I was trying to get at and use the week as a grouping.
select ID, Hours, Date, DATEPART(wk, Date) WeekNumber
from ....
or if you were summarizing by week, you could put
select sum(Hours) TotalHours, DATEPART(wk, Date) WeekNumber
from ....
group by DATEPART(wk, Date)