create table table1
(
yourdate datetime ,
yourvalue int
)
insert into table1 values ('2005-12-01 10:00:00.000',1)
insert into table1 values ('2005-12-01 20:00:00.000',2)
insert into table1 values ('2005-12-01 01:00:00.000',3)
insert into table1 values ('2005-12-02 01:00:00.000',4)
insert into table1 values ('2006-01-01 02:00:00.000',5)
select year(yourdate) as Year,month(yourdate) as Month,day(yourdate) as Day,
SUM(case when DATEPART(hh, yourdate) = 0 then yourvalue else 0 end) '0',
SUM(case when DATEPART(hh, yourdate) = 1 then yourvalue else 0 end) '1',
SUM(case when DATEPART(hh, yourdate) = 2 then yourvalue else 0 end) '2',
SUM(case when DATEPART(hh, yourdate) = 3 then yourvalue else 0 end) '3',
SUM(case when DATEPART(hh, yourdate) = 4 then yourvalue else 0 end) '4',
SUM(case when DATEPART(hh, yourdate) = 5 then yourvalue else 0 end) '5',
SUM(case when DATEPART(hh, yourdate) = 6 then yourvalue else 0 end) '6',
SUM(case when DATEPART(hh, yourdate) = 7 then yourvalue else 0 end) '7',
SUM(case when DATEPART(hh, yourdate) = 8 then yourvalue else 0 end) '8',
SUM(case when DATEPART(hh, yourdate) = 9 then yourvalue else 0 end) '9',
SUM(case when DATEPART(hh, yourdate) = 10 then yourvalue else 0 end) '10',
SUM(case when DATEPART(hh, yourdate) = 11 then yourvalue else 0 end) '11',
SUM(case when DATEPART(hh, yourdate) = 12 then yourvalue else 0 end) '12',
SUM(case when DATEPART(hh, yourdate) = 13 then yourvalue else 0 end) '13',
SUM(case when DATEPART(hh, yourdate) = 14 then yourvalue else 0 end) '14',
SUM(case when DATEPART(hh, yourdate) = 15 then yourvalue else 0 end) '15',
SUM(case when DATEPART(hh, yourdate) = 16 then yourvalue else 0 end) '16',
SUM(case when DATEPART(hh, yourdate) = 17 then yourvalue else 0 end) '17',
SUM(case when DATEPART(hh, yourdate) = 18 then yourvalue else 0 end) '18',
SUM(case when DATEPART(hh, yourdate) = 19 then yourvalue else 0 end) '19',
SUM(case when DATEPART(hh, yourdate) = 20 then yourvalue else 0 end) '20',
SUM(case when DATEPART(hh, yourdate) = 21 then yourvalue else 0 end) '21',
SUM(case when DATEPART(hh, yourdate) = 22 then yourvalue else 0 end) '22',
SUM(case when DATEPART(hh, yourdate) = 23 then yourvalue else 0 end) '23'
from table1
group by year(yourdate),month(yourdate),day(yourdate)
go
drop table table1