SQL Pivot table for each hour -
i have table below
prefix scandate 1/2/2013 08:30 1/2/2013 08:45 b 1/2/2103 09:15
i need output count in every hour i,e
time b 08:30-09:30 2 0 09:30-10-30 0 1
till 7pm
can 1 me
depending on db using, should works:
select sum(case when datepart(hh, dateadd(m, 30, scandate) = 1 1 else 0 end) '00:30 - 01:30 block' sum(case when datepart(hh, dateadd(m, 30, scandate) = 2 1 else 0 end) '01:30 - 02:30 block' sum(case when datepart(hh, dateadd(m, 30, scandate) = 3 1 else 0 end) '02:30 - 03:30 block' ... table
you'll need iterate out hour part identifiers until 7 pm i.e. 1, 2,...19. caveat, won't work times before 12:30 am.
Comments
Post a Comment