sql server - Group and count by 16 hours time interval not working -
i trying number of records 16 hour time interval. below code using now.
;with cte_hours ( --hours generation select top(6) hr = (row_number() on (order (select null))-1)*4 master..spt_values ), cte2 ( --getting range select dateadd(hh, c.hr, convert(datetime,d.dts) ) dts_start, dateadd(ms, -2, dateadd(hh, c.hr+ 4, convert(datetime,d.dts) ) ) dts_end (select distinct convert(date, dt) dts test2 ) d cross apply cte_hours c ) --actual query select c2.dts_start dt, sum(case when t.dt not null 1 else 0 end) no_of_records,ld_voy_n,ld_vsl_m cte2 c2 left join test2 t on t.dt between c2.dts_start , c2.dts_end group c2.dts_start,ld_voy_n,ld_vsl_m order ld_voy_n, ld_vsl_m, dts_start asc
this code able count number of records have based on 4,6, , 12 hour interval. however, if try count based on 16 hour interval, somehow not work. below code , output used 16 hour interval.
;with cte_hours ( --hours generation select top(6) hr = (row_number() on (order (select null))-1)*16 master..spt_values ), cte2 ( --getting range select dateadd(hh, c.hr, convert(datetime,d.dts) ) dts_start, dateadd(ms, -2, dateadd(hh, c.hr+ 16, convert(datetime,d.dts) ) ) dts_end (select distinct convert(date, dt) dts test2 ) d cross apply cte_hours c ) --actual query select c2.dts_start dt, sum(case when t.dt not null 1 else 0 end) no_of_records,ld_voy_n,ld_vsl_m cte2 c2 left join test2 t on t.dt between c2.dts_start , c2.dts_end group c2.dts_start,ld_voy_n,ld_vsl_m order ld_voy_n, ld_vsl_m, dts_start asc
result:
dt no_of_records ld_voy_n ld_vsl_m 2017-05-05 16:00:00.000 14 0002w pqo emzmnwp 2017-05-06 00:00:00.000 14 0002w pqo emzmnwp 2017-05-06 08:00:00.000 12 0002w pqo emzmnwp 2017-05-06 16:00:00.000 12 0002w pqo emzmnwp 2017-05-01 16:00:00.000 1 0007e omq ynzmeoyn 2017-05-02 00:00:00.000 1 0007e omq ynzmeoyn
it taking 8 hour timing well. of have idea why?
Comments
Post a Comment