I have an interesting grouping case for which I really can't figure the best way to proceed with. I'm dealing with a lot of data so I'm trying to find the most effective way to perform my query.
Here's an example of data I could have
|Line |Trip |Speed |Timestamp
====================================================
|100 |1 |50 kmh |2017-06-12 22:34:50
|100 |1 |55 kmh |2017-06-12 22:36:44
|100 |1 |56 kmh |2017-06-12 22:37:12
|200 |5 |12 kmh |2017-06-12 22:40:11
|200 |5 |18 kmh |2017-06-12 22:43:13
|100 |1 |23 kmh |2017-06-12 22:49:11
|100 |1 |45 kmh |2017-06-12 22:53:49
I would like to be able to define a grouping number sequentially based on the Line & Trip ordered by the timestamp. So basically at the end I would expect something such as:
|Line |Trip |Speed |Timestamp |GroupNumber
===========================================================================
|100 |1 |50 kmh |2017-06-12 22:34:50 | 1
|100 |1 |55 kmh |2017-06-12 22:36:44 | 1
|100 |1 |56 kmh |2017-06-12 22:37:12 | 1
|200 |5 |12 kmh |2017-06-12 22:40:11 | 2
|200 |5 |18 kmh |2017-06-12 22:43:13 | 2
|100 |1 |23 kmh |2017-06-12 22:49:11 | 3
|100 |1 |45 kmh |2017-06-12 22:53:49 | 3
I can't find any way using DENSE_RANK or ROW_NUMBER because the last group of 100/1 is merged again with the first records but should not because there is another group (200/5) between both occurence.
Any help would be appreciated. Thank you.