I have a requirement to get the difference between on and off timestamps based on a condition. The timestamps are from the same column but the condition determines whether it is on or off. I need my result to display consecutive on and off timestamp in same row and their difference. Below query works in Maria DB but fails in Informix since subquery with limit are not permitted in select statement. Using: IBM Informix Developer Edition V11.70. I will be able to calculate the time difference but needing help with converting this query to be Informix and HQL compatible. Here is my code snippet:
SELECT
rec_id,
last_change_ts as ontime,
(SELECT first last_change_ts
FROM trailer_log t_out
WHERE t_out.rec_id = t_in.rec_id
AND t_out.last_change_ts >= t_in.last_change_ts
AND t_out.old_value = 'OH'
ORDER BY last_change_ts LIMIT 1
) AS offtime
FROM
trailer_log t_in
WHERE
new_value='OH'
ORDER BY
rec_id, last_change_ts
What would be the equivalent of doing this in Informix with HQL? Sample input data:
rec_id last_change_ts new_value old_value
1188123 2017-10-09 12:37:46 OH IN
1188123 2017-10-09 12:44:35 IN OH
1188123 2017-10-09 12:57:12 OH IN
1188123 2017-10-10 10:38:01 MT OH
1188123 2017-10-10 10:44:30 OH MT
1188133 2017-10-13 12:15:29 OH HT
1188133 2017-10-13 12:24:07 HT OH
1188143 2017-10-25 13:10:07 OH MT
1188143 2017-10-25 13:15:56 MT OH
1188148 2017-10-27 18:48:03 OH HT
1188148 2017-10-31 17:27:09 SL OH
1188150 2017-10-31 18:57:21 OH HT
1188151 2017-11-09 07:57:51 OH MT
1188151 2017-11-09 08:00:13 MT OH
1188152 2017-11-10 14:56:48 OH PL
1188152 2017-11-10 15:27:29 PL OH
Expected output:
rec_id ontime offtime
1188123 10/9/2017 12:37 10/9/2017 12:44
1188123 10/9/2017 12:57 10/10/2017 10:38
1188123 10/10/2017 10:44
1188133 10/13/2017 12:15 10/13/2017 12:24
1188143 10/25/2017 13:10 10/25/2017 13:15
1188148 10/27/2017 18:48 10/31/2017 17:27
1188150 10/31/2017 18:57
1188151 11/9/2017 7:57 11/9/2017 8:00
1188152 11/10/2017 14:56 11/10/2017 15:27