This is easy to do if you have a date table.Don't have a date table? You can get the Mother of All Date Tables function, F_TABLE_DATE, from the Script Library forum:http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=61519declare @start datetime, @end datetimeselect @start = '2006-03-01'select @end = '2006-03-31'-- Table for test datadeclare @INTran table (TranDate datetime not null, QTY int not null)-- Load test datainsert into @INTranselect DATE, -- Generate a random number from 0 to 999 QTY = abs(convert(int,convert(varbinary(20),newid()))%1000)from dbo.F_TABLE_DATE(@start,@end)where -- No sunday sales DAY_OF_WEEK <> 1-- Return results grouped by weekSelect FROM_DATE = min(a.DATE), TO_DATE = max(a.DATE), QTY = sum(isnull(b.QTY,0))from dbo.F_TABLE_DATE(@start,@end) a left join @INTran b on a.DATE = b.TranDategroup by a.WEEK_STARTING_SUN_SEQ_NOorder by a.WEEK_STARTING_SUN_SEQ_NO
Results:FROM_DATE TO_DATE QTY----------------------- ----------------------- ----------2006-03-01 00:00:00.000 2006-03-04 00:00:00.000 27112006-03-05 00:00:00.000 2006-03-11 00:00:00.000 35942006-03-12 00:00:00.000 2006-03-18 00:00:00.000 26392006-03-19 00:00:00.000 2006-03-25 00:00:00.000 25552006-03-26 00:00:00.000 2006-03-31 00:00:00.000 2713(5 row(s) affected)
CODO ERGO SUM