I have a table as shown below. I have column that tracks an application and application status changes over time. I track the time it changes the status in the date column. The table is sorted by application and date of status change from oldest to newest.

```

+--------+-----------+--------+------------------+
| app_id | status_id | row_no |       date       |
+--------+-----------+--------+------------------+
|      1 | a         |     10 | 2016-10-04 21:35 |
|      1 | b         |     11 | 2016-10-12 21:50 |
|      1 | c         |     12 | 2016-10-25 20:40 |
|      1 | d         |     13 | 2016-10-26 16:10 |
|      1 | e         |     14 | 2016-10-26 16:10 |
|      2 | a         |     20 | 2016-09-15 1:26  |
|      2 | c         |     21 | 2016-09-15 21:32 |
|      2 | d         |     22 | 2016-09-16 21:51 |
|      2 | e         |     23 | 2016-09-16 21:51 |
|      2 | f         |     24 | 2016-09-20 22:55 |
|      2 | g         |     25 | 2016-10-20 22:46 |
|      2 | g         |     26 | 2016-10-20 22:46 |
+--------+-----------+--------+------------------+

```

I am trying to achieve how much time an application is spending before it reached the final state. Below is a sample of the table That i am trying to build in Sql. For every status i am trying to capture the next status. The previous status column shows the status in that row, while next status shows the next status in the next row for that application. If the application is at its last status then the next status is marked as Last. Next I calculate the time between status by calculating the hours differences between two dates. I would really appreciate if you can tell me how to achieve this functionality in Sql. Thank you in advance.

```

+--------+-----------+--------+------------------+-----------------+-------------+--------------+
| app_id | status_id | row_no |       date       | previous status | next status | time between |
+--------+-----------+--------+------------------+-----------------+-------------+--------------+
|      1 | a         |     10 | 2016-10-04 21:35 | a               | b           | 192.2333333  |
|      1 | b         |     11 | 2016-10-12 21:50 | b               | c           | 334.8333333  |
|      1 | c         |     12 | 2016-10-25 20:40 | c               | d           | 43.48333333  |
|      1 | d         |     13 | 2016-10-26 16:10 | d               | e           | 0            |
|      1 | e         |     14 | 2016-10-26 16:10 | e               | Last        | Last         |
|      2 | a         |     20 | 2016-09-15 1:26  | a               | c           | 20.08333333  |
|      2 | c         |     21 | 2016-09-15 21:32 | c               | d           | 24.31666667  |
|      2 | d         |     22 | 2016-09-16 21:51 | d               | e           | 0            |
|      2 | e         |     23 | 2016-09-16 21:51 | e               | f           | 97.06666667  |
|      2 | f         |     24 | 2016-09-20 22:55 | f               | g           | 743.8333333  |
|      2 | g         |     25 | 2016-10-20 22:46 | g               | g           | 0            |
|      2 | g         |     26 | 2016-10-20 22:46 | g               | Last        | Last         |
+--------+-----------+--------+------------------+-----------------+-------------+--------------+

```