Well, you need start with either a calender table or a table of numbers or a function that returns a table of dates or numbers, then LEFT OUTER JOIN to your table with the dates. Change the group by to use the numbers and aggregate from your table with the sparse data. Here is an example using the same example as before but including a table from MASTER database which happens to contain numbers.declare @start datetime ,@end datetimeselect @start = getdate() - 33 ,@end = getdate() + 14;with [yourTableWithDates] (dt)as(--table of dates select getdate()-33 union all select dateadd(day, 1, dt) from [yourTableWithDates] where datediff(day, getdate(), dt) < 1)select dateadd(week, n.number, @start) [from_date] ,dateadd(week, n.number, @start) + 6 [end_date] ,count(t.dt) [day_count_of_group]from master..spt_values nleft outer join [yourTableWithDates] t on t.dt >= dateadd(week, n.number, @start) and t.dt < dateadd(week, n.number, @start) + 7where n.type = 'P'and n.number < datediff(week, @start, @end)group by dateadd(week, n.number, @start)OUTPUT:from_date end_date day_count_of_group----------------------- ----------------------- ------------------2009-08-27 13:48:08.440 2009-09-02 13:48:08.440 72009-09-03 13:48:08.440 2009-09-09 13:48:08.440 72009-09-10 13:48:08.440 2009-09-16 13:48:08.440 72009-09-17 13:48:08.440 2009-09-23 13:48:08.440 72009-09-24 13:48:08.440 2009-09-30 13:48:08.440 7 2009-10-01 13:48:08.440 2009-10-07 13:48:08.440 02009-10-08 13:48:08.440 2009-10-14 13:48:08.440 0
Be One with the OptimizerTG