Home Forums SQL Server 2012 SQL 2012 - General Help with SQL - Calculate time difference for consecutive rows RE: Help with SQL - Calculate time difference for consecutive rows

  • Nicely formatted expected results from the OPs initial post:

    S_ID S_ACTV_CODE_PREV S_ACTV_CODE_CURR S_USER S_START_TIME S_END_TIME TIME_SPENT (in Sec)

    AAA-111 NULL NULL USER1 2015-06-15 00:21:06 2015-06-15 00:21:07 1

    AAA-111 NULL 2 USER1 2015-06-15 00:21:07 2015-06-17 03:20:33 183566

    AAA-111 2 4 USER2 2015-06-17 03:20:33 2015-06-17 03:43:25 1372

    AAA-111 4 3 USER3 2015-06-17 03:43:25 2015-06-22 05:02:37 436752

    AAA-111 3 4 USER4 2015-06-22 05:02:37 NULL NULL

    AAA-112 NULL NULL USER4 2015-06-25 11:11:11 2015-06-25 11:11:12 1

    AAA-112 NULL 4 USER3 2015-06-25 11:11:12 NULL NULL

    AAA-113 NULL NULL USER2 2015-06-24 07:10:37 2015-06-24 07:10:43 6

    AAA-113 NULL 3 USER1 2015-06-24 07:10:43 NULL NULL

    Edit:

    Maybe the sort order and crooked eyes on my part.