ID | StartTime | StartLocation | EndTime | EndLocation |
---|---|---|---|---|
1 | 2023-10-15 10:00:00 | A | 2023-10-15 12:10:00 | B |
1 | 2023-10-15 13:00:00 | B | 2023-10-15 18:00:00 | A |
I want to merge these two rows into one so I get the first StartLocation and the last EndLocation. It should like this:
ID | StartTime | StartLocation | EndTime | EndLocation |
---|---|---|---|---|
1 | 2023-10-15 10:00:00 | A | 2023-10-15 18:00:00 | A |
2
Answers
Using
ROW_NUMBER
we can try:Demo
If the amount of rows may be more than 2 then you’d use recursive CTE and build complete rows chain.