SQL Server: query to get the data between two values from same columns and calculate time difference -
i have requirement number of hours between 2 values, 20 , 25 or above (this user input values , not fixed). below table sample data.
consider in table on 01-09-2016 08:40
value_id
25
, reaches 20
on 02-09-2016 13:20
, need consider number of hours between these 2 range ie 12 hours , 40 min .. 04-09-2016 13:20
reached 26.3
(which above 25 ) , '06-09-2016 16:20'
reached 19.3
(below 20) , number of hours 45 hours. tried creating function, it's not working..
code create table:
create table [dbo].[numofhrs]( [id] [float] null, [date] [datetime] null, [value_id] [float] null ) on [primary]
code insert data :
insert [dbo].[numofhrs] ([id] ,[date] ,[value_id]) values (112233,'8-31-2016 08:20:00',19.2), (112233,'9-01-2016 08:30:00',24), (112233,'9-01-2016 08:40:00',25), (112233,'9-01-2016 09:20:00',26), (112233,'9-02-2016 10:20:00',27), (112233,'9-02-2016 10:20:00',24), (112233,'9-02-2016 10:20:00',23), (112233,'9-02-2016 11:20:00',22), (112233,'9-02-2016 12:20:00',21), (112233,'9-02-2016 13:20:00',20), (112233,'9-03-2016 13:20:00',19.8), (112233,'9-04-2016 13:20:00',21), (112233,'9-04-2016 14:20:00',24), (112233,'9-04-2016 16:20:00',24.6), (112233,'9-04-2016 19:20:00',26.3), (112233,'9-04-2016 23:20:00',27), (112233,'9-05-2016 00:20:00',22), (112233,'9-06-2016 16:20:00',19.3), (112233,'9-07-2016 00:20:00',22), (112233,'9-08-2016 00:20:00',21), (112233,'9-09-2016 00:20:00',23), (445566,'9-10-2016 00:20:00',24), (445566,'9-11-2016 00:20:00',25), (445566,'9-12-2016 00:20:00',26), (445566,'9-13-2016 00:20:00',24), (445566,'9-14-2016 00:20:00',23), (445566,'9-15-2016 00:20:00',24), (445566,'9-16-2016 00:20:00',21), (445566,'9-17-2016 00:20:00',20), (445566,'9-18-2016 00:20:00',18.5), (445566,'9-19-2016 00:20:00',17)
well, couldn't think of simpler. here's try solve problem:
;with numofhrs_rn ( select id, [date], value_id, row_number() on (partition id order [date]) rn [dbo].[numofhrs] ), numofhrs_lag ( select t1.id, t1.[date], t2.value_id prev_value, t1.value_id curr_value numofhrs_rn t1 -- previous value (lag) join numofhrs_rn t2 on t1.id = t2.id , t1.rn = t2.rn + 1 ), numofhrs_flag ( select id, [date], prev_value, curr_value, case when curr_value >= 25 , prev_value < 25 'start' when curr_value <= 20 , prev_value > 20 'stop' else 'ignore' end flag numofhrs_lag ), numofhrs_grp ( select id, [date], curr_value, flag, row_number() on (partition id order [date]) - case flag when 'start' 0 when 'stop' 1 end grp numofhrs_flag flag in ('start', 'stop') ) select min([date]) 'start', max([date]) 'stop' numofhrs_grp group id, grp order min([date])
output:
start stop ------------------------------------------------ 2016-09-01 08:40:00.000 2016-09-02 13:20:00.000 2016-09-04 19:20:00.000 2016-09-06 16:20:00.000 2016-09-11 00:20:00.000 2016-09-17 00:20:00.000
you can manipulate above query in order time difference expressed in hours/minutes/seconds format.
Comments
Post a Comment