How to Calculate the Time Between Two Rows in SQL Server
I had an interesting problem to solve: How long did a task take to complete? The challenge was that our task history table contains one row for the start and another one for the end of a task:
| Id | TaskId | Action | Timestamp |
|---|---|---|---|
| 1 | 100 | Start | 2022-08-01 08:00:00.000 |
| 2 | 101 | Start | 2022-08-02 07:30:00.000 |
| 3 | 100 | Stop | 2022-08-03 17:00:00.000 |
| 4 | 102 | Start | 2022-08-04 08:00:00.000 |
| 5 | 102 | Stop | 2022-08-04 17:00:00.000 |
| 6 | 101 | Stop | 2022-08-05 18:00:00.000 |
