Вот один из способов. Это выглядит довольно глупо, но тогда мне всегда нравятся оси.
DECLARE @Saturday datetime
SET @Saturday = 'Aug 7, 2010'
SELECT
@Saturday WeekEnding
,Project
,Category
,isnull([1], 0) SunHrs
,isnull([2], 0) MonHrs
,isnull([3], 0) TueHrs
,isnull([4], 0) WedHrs
,isnull([5], 0) ThuHrs
,isnull([6], 0) FriHrs
,isnull([7], 0) SatHrs
from (select Project, Category, Datepart(dw, Date) DOW, Hours
from MyTable
-- Fixed bug from -7 to -6
where Date between dateadd(dd, /*-7*/ -6, @Saturday) and @Saturday) Source
pivot (max(Hours)
for DOW in ([1],[2],[3],[4],[5],[6],[7]) ) as pvt
Я использовал следующее, чтобы настроить данные для проверки:
DROP TABLE MyTable
CREATE TABLE MyTable
(
Project varchar(10) not null
,Category varchar(10) not null
,Date datetime not null
,Hours int not null
)
INSERT MyTable
values
('Proj1', 'test', '8/2/2010', 2 ),
('Proj1', 'test', '8/3/2010', 8 ),
('Proj1', 'test', '8/4/2010', 4 ),
('Proj1', 'test', '8/5/2010', 3 ),
('Proj1', 'test', '8/6/2010', 5 )