Can Analytical function be used to collapse these record’s
from this
ssid start_time end_time op_code
65F 08/JAN/2013 14:18:33 08/JAN/2013 14:18:34 2
65F 08/JAN/2013 14:18:53 08/JAN/2013 14:18:54 2
65F 08/JAN/2013 14:18:55 08/JAN/2013 14:18:59 3
65F 08/JAN/2013 14:19:33 08/JAN/2013 14:19:34 2
65F 08/JAN/2013 14:19:53 08/JAN/2013 14:19:54 2
65F 08/JAN/2013 14:19:55 08/JAN/2013 14:19:59 3
72F 08/JAN/2013 14:20:55 08/JAN/2013 14:20:56 2
72F 08/JAN/2013 14:19:57 08/JAN/2013 14:19:59 2
to this
ssid start_time end_time c_dt op_code
65F 08/JAN/2013 14:18:33 08/JAN/2013 14:18:54 08/JAN/2013 14:18:59 2
65F 08/JAN/2013 14:19:33 08/JAN/2013 14:19:54 08/JAN/2013 14:19:59 2
72F 08/JAN/2013 14:19:57 08/JAN/2013 14:20:56
or i have to processes this in a for loop only .
I am on Oracle 11gR2
If the next op_code is 2
then the end_time becomes the end_time of the next record and we go on replacing the end
time of this record until a same ssid with op_code 3 arrives.
and when the record with the op_code 3 arrives we generate the c_dt which is the end_time of record with same ssid but with op_code 3.
we start this process all over again for the next record
If we do not find a record with op_code 3 for a particular ssid then the c_dt will be null
but the end_time will be the end_time of the next record with same ssid.
Hope i am clear on this tricky logic
I’ll be the first to say that this is NOT pretty, but it should get you going in the right direction.
First, create a CTE with all your records with a corresponding row number.
Second, create another CTE which has the next record where op code is not the same.
Third, create another CTE which has the min row number, max row number, and next row number (potentially null) and use this to get your results.
It very well could be simplified, but this was my first attempt.
And here is the SQL Fiddle.
Good luck.