I have a table with two columns: the action start time and the name of action.
time | action |
---|---|
2023-07-27 04:52:00.000 | running |
2023-07-27 04:55:00.000 | walking |
2023-07-27 04:59:00.000 | walking |
2023-07-27 05:01:00.000 | sitting |
2023-07-27 05:06:00.000 | walking |
2023-07-27 05:10:00.000 | running |
I need to know the duration of the action: start and end time. The start time is the time of the action, which is already known.
But how to know the end time? That is, the beginning of the next action different from the current one?
Example for the action "walking"
time_start | time_end |
---|---|
2023-07-27 04:55:00.000 | 2023-07-27 05:01:00.000 |
2023-07-27 05:06:00.000 | 2023-07-27 05:10:00.000 |
2
Answers
Using the difference in row numbers method we can try the following:
Demo
I think you could use the lag function to achieve your goal.
In this case the time_end for the last row will be equal to NULL, which I think is correct, as it isn’t known yet.