Function F_TABLE_DATE is a table valued function, so it can be used like a table in a query.The query below shows how you can use it to do the crosstab you want.declare @start_date datetimedeclare @end_date datetimeset @start_date = '20061101'set @end_date = '20061130'declare @t table (TimeoffID int not null,employeename varchar(10) not null,startdate datetime not null,enddate datetime not null,timeofftype varchar(10) not null)insert into @tselect 1, 'Andy', '11/02/2006', '11/03/2006', 'Sick Day' union allselect 2, 'Bill', '11/03/2006', '11/03/2006', 'Sick Day'select [Monday] = sum(case when b.DAY_OF_WEEK = 2 then 1 else 0 end) , [Tuesday] = sum(case when b.DAY_OF_WEEK = 3 then 1 else 0 end) , [Wednesday] = sum(case when b.DAY_OF_WEEK = 4 then 1 else 0 end) , [Thursday] = sum(case when b.DAY_OF_WEEK = 5 then 1 else 0 end) , [Friday] = sum(case when b.DAY_OF_WEEK = 6 then 1 else 0 end) , [Saturday] = sum(case when b.DAY_OF_WEEK = 7 then 1 else 0 end) , [Sunday] = sum(case when b.DAY_OF_WEEK = 1 then 1 else 0 end)from @t a join F_TABLE_DATE(@start_date,@end_date ) b on b.DATE between a.startdate and a.enddate
Results:(2 row(s) affected)Monday Tuesday Wednesday Thursday Friday Saturday Sunday ----------- ----------- ----------- ----------- ----------- ----------- ----------- 0 0 0 1 2 0 0(1 row(s) affected)
CODO ERGO SUM