sql - How to count every half hour? -
i have query counting every hour, using pivot table. how possible count every 30 minutes?
for example 8:00-8:29,8:30-8:59,9:00-9:29 etc. until 5:00
select convert(varchar(8),start_date,1) 'day', sum(case when datepart(hour,start_date) = 8 1 else 0 end) 8 , sum(case when datepart(hour,start_date) = 9 1 else 0 end) nine, sum(case when datepart(hour,start_date) = 10 1 else 0 end) ten, sum(case when datepart(hour,start_date) = 11 1 else 0 end) eleven, sum(case when datepart(hour,start_date) = 12 1 else 0 end) twelve, sum(case when datepart(hour,start_date) = 13 1 else 0 end) one_clock, sum(case when datepart(hour,start_date) = 14 1 else 0 end) two_clock, sum(case when datepart(hour,start_date) = 15 1 else 0 end) three_clock, sum(case when datepart(hour,start_date) = 16 1 else 0 end) four_clock test user_id not null group convert(varchar(8),start_date,1) order convert(varchar(8),start_date,1)
i use sql server 2012 (version microsoft sql server management studio 11.0.3128.0)
try using iif
below:
select convert(varchar(8),start_date,1) 'day', sum(iif(datepart(hour,start_date) = 8 , datepart(minute,start_date) >= 0 , datepart(minute,start_date) =< 29,1,0)) eight_tirty test user_id not null group convert(varchar(8),start_date,1) order convert(varchar(8),start_date,1)
Comments
Post a Comment