Have your frontend program show correct column names and filter out last 4 columns if time interval is 3 hours.

Otherwise you'd have to write dynamic sql, which is to be avoided where ever you can.

Give this a try:

```
declare @start_hh time=cast('06:00' as time);
declare @start_dt datetime=cast('2017-01-25' as datetime)+cast(@start_hh as datetime);
declare @end_dt datetime=cast('2017-02-20' as datetime)+cast(@start_hh as datetime);
declare @time_step int=2;
with tally_date(start_dt,end_dt) /* 1000 days (aprox 3 year) */
as (select dateadd(day,row_number() over(order by n1.n)-1,@start_dt) as start_dt
,dateadd(day,row_number() over(order by n1.n),@start_dt) as end_dt
from (values(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) as n1(n)
cross apply (values(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) as n2(n)
cross apply (values(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) as n3(n)
)
select cast(b.entereddatetime as date) as dt
,sum(case when b.entereddatetime>=dateadd(hour,0*@time_step,a.start_dt) and b.entereddatetime<dateadd(hour,1*@time_step,a.start_dt) then 1 else 0 end) as period_1
,sum(case when b.entereddatetime>=dateadd(hour,1*@time_step,a.start_dt) and b.entereddatetime<dateadd(hour,2*@time_step,a.start_dt) then 1 else 0 end) as period_2
,sum(case when b.entereddatetime>=dateadd(hour,2*@time_step,a.start_dt) and b.entereddatetime<dateadd(hour,3*@time_step,a.start_dt) then 1 else 0 end) as period_3
,sum(case when b.entereddatetime>=dateadd(hour,3*@time_step,a.start_dt) and b.entereddatetime<dateadd(hour,4*@time_step,a.start_dt) then 1 else 0 end) as period_4
,sum(case when b.entereddatetime>=dateadd(hour,4*@time_step,a.start_dt) and b.entereddatetime<dateadd(hour,5*@time_step,a.start_dt) then 1 else 0 end) as period_5
,sum(case when b.entereddatetime>=dateadd(hour,5*@time_step,a.start_dt) and b.entereddatetime<dateadd(hour,6*@time_step,a.start_dt) then 1 else 0 end) as period_6
,sum(case when b.entereddatetime>=dateadd(hour,6*@time_step,a.start_dt) and b.entereddatetime<dateadd(hour,7*@time_step,a.start_dt) then 1 else 0 end) as period_7
,sum(case when b.entereddatetime>=dateadd(hour,7*@time_step,a.start_dt) and b.entereddatetime<dateadd(hour,8*@time_step,a.start_dt) then 1 else 0 end) as period_8
,sum(case when b.entereddatetime>=dateadd(hour,8*@time_step,a.start_dt) and b.entereddatetime<dateadd(hour,9*@time_step,a.start_dt) then 1 else 0 end) as period_9
,sum(case when b.entereddatetime>=dateadd(hour,9*@time_step,a.start_dt) and b.entereddatetime<dateadd(hour,10*@time_step,a.start_dt) then 1 else 0 end) as period_10
,sum(case when b.entereddatetime>=dateadd(hour,10*@time_step,a.start_dt) and b.entereddatetime<dateadd(hour,11*@time_step,a.start_dt) then 1 else 0 end) as period_11
,sum(case when b.entereddatetime>=dateadd(hour,11*@time_step,a.start_dt) and b.entereddatetime<dateadd(hour,12*@time_step,a.start_dt) then 1 else 0 end) as period_12
from tally_date as a
inner join yourtable as b
on b.entereddatetime>=a.start_dt
and b.entereddatetime<a.end_dt
where a.end_dt<@end_dt+2
group by cast(b.entereddatetime as date)
;
```