I have a table in SQL Server 2012 with events log in a format like this:
+=====+=============================+=============================+======+
| ID1 | start_time_utc | end_time_utc | ID2 |
+=====+=============================+=============================+======+
| 57 | 2018-11-11 11:00:00.0000000 | 2018-11-11 11:00:28.0012900 | 15 |
| 57 | 2018-11-11 11:00:28.0012900 | 2018-11-11 11:01:29.0543947 | 1020 |
| 57 | 2018-11-11 11:01:29.0543947 | 2018-11-11 11:02:28.1923079 | 16 |
| 57 | 2018-11-11 11:02:28.1923079 | 2018-11-11 11:04:28.3367626 | 16 |
| 57 | 2018-11-11 11:04:28.3367626 | 2018-11-11 11:05:28.5307626 | 1020 |
| 103 | 2018-11-10 20:00:00.0000000 | 2018-11-11 03:00:00.0000000 | 15 |
| 103 | 2018-11-11 03:00:00.0000000 | 2018-11-11 10:57:00.8175737 | 15 |
| 103 | 2018-11-11 10:57:00.8175737 | 2018-11-11 10:57:27.8322749 | 1017 |
| 103 | 2018-11-11 10:57:27.8322749 | 2018-11-11 11:00:00.0000000 | 15 |
| 103 | 2018-11-11 11:00:00.0000000 | 2018-11-11 11:00:31.9916890 | 15 |
+-----+-----------------------------+-----------------------------+------+
For a given ID1, end date has a matching start date of the next event. I want to partition the data by matching ID1 and ID2 columns, and select start date and end date for each partition of consecutive events. So the result should be:
+=====+=============================+=============================+======+
| ID1 | start_time_utc | end_time_utc | ID2 |
+=====+=============================+=============================+======+
| 57 | 2018-11-11 11:00:00.0000000 | 2018-11-11 11:00:28.0012900 | 15 |
| 57 | 2018-11-11 11:00:28.0012900 | 2018-11-11 11:01:29.0543947 | 1020 |
| 57 | 2018-11-11 11:01:29.0543947 | 2018-11-11 11:04:28.3367626 | 16 |
| 57 | 2018-11-11 11:04:28.3367626 | 2018-11-11 11:05:28.5307626 | 1020 |
| 103 | 2018-11-10 20:00:00.0000000 | 2018-11-11 10:57:00.8175737 | 15 |
| 103 | 2018-11-11 10:57:00.8175737 | 2018-11-11 10:57:27.8322749 | 1017 |
| 103 | 2018-11-11 10:57:27.8322749 | 2018-11-11 11:00:31.9916890 | 15 |
+-----+-----------------------------+-----------------------------+------+
I obviously can't use simple group by and don't know how to write the partition by query. Thanks for help.