I have a select Statement where 3 columns and n-rows like:
number of the equipment time some count
0128 2015-05-26 20:31:57.000 1
0128 2015-05-27 06:53:12.000 1
0128 2015-05-27 06:58:04.000 1
How can I find datediff
between these times
if records was 2 it would be possible to remove in one line as max
and min
time but number of records always different
I need to compare. for example case when datediff(mm,time1,time2) < 60 min then
..... how can i compare time1-time2
, time2 - time3
if the number of records constantly changes
my code
DECLARE @start_date DATE, @end_date DATE, @equip varchar(4), @shift_ident varchar(2)
SET @start_date = '2015-05-27'
SET @end_date = '2015-05-27'
SET @shift_ident = '1'
SET @equip ='0128'
;with cte as (SELECT ROW_NUMBER() over(ORDER BY START_TIMESTAMP) num,
EQUIP_IDENT as eq ,
START_TIMESTAMP as time,
count(STATUS_CODE) as Zapravka
from EQUIPMENT_STATUS_TRANS
WHERE STATUS_CODE = 'O23'
AND SHIFT_DATE between @start_date and @end_date
AND @shift_ident LIKE '%' + SHIFT_IDENT + '%'
AND EQUIP_IDENT = @equip
GROUP BY EQUIP_IDENT,START_TIMESTAMP)
SELECT est.time, DATEDIFF(mm, est.time,est2.time) as duration
from cte est
join cte est2 on est.num = est2.num-1
and result
2015-05-26 20:31:57.000 0
2015-05-27 06:53:12.000 0
duration = 0... why?