Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- WITH Level1
- AS (-- apply row numbers
- SELECT emp_reader_id ,
- CAST(dt AS DATETIME) AS [DATETIME] ,
- ROW_NUMBER() OVER ( PARTITION BY emp_reader_id
- ORDER BY dt ) AS RowNum
- FROM trnevents
- ),
- LEVEL2
- AS (-- find the last and next event type for each row
- SELECT A.emp_reader_id ,
- A.DATETIME ,
- COALESCE(NULL, 'N/A') AS LastEvent ,
- COALESCE(NULL, 'N/A') AS NextEvent
- FROM Level1 A
- LEFT JOIN Level1 LastVal
- ON A.emp_reader_id = LastVal.emp_reader_id
- AND A.RowNum - 1 = LastVal.RowNum
- LEFT JOIN Level1 NextVal
- ON A.emp_reader_id = NextVal.emp_reader_id
- AND A.RowNum + 1 = NextVal.RowNum ),
- Level3
- AS (-- reapply row numbers to row-eliminated set
- SELECT emp_reader_id ,
- DATETIME ,
- LastEvent ,
- NextEvent ,
- ROW_NUMBER() OVER ( PARTITION BY emp_reader_id
- ORDER BY DATETIME ) AS RowNBr
- FROM Level2
- ),
- Level4
- AS (-- pair enter and exit rows.
- SELECT A.emp_reader_id ,
- A.DATETIME ,
- B.DATETIME AS ExitDATETIME
- FROM Level3 A
- JOIN Level3 B ON A.emp_reader_id = B.emp_reader_id
- AND A.RowNBr + 1 = B.RowNBr
- ),
- LEVEL5
- AS (--Calculate the work session duration
- SELECT emp_reader_id ,
- DATEDIFF(second, DATETIME, ExitDATETIME)
- AS Seconds ,
- DATETIME ,
- ExitDATETIME
- FROM Level4
- )
- SELECT emp_reader_id ,
- CAST([DATETIME] AS date) AS [dt],
- CAST([DATETIME] AS Time) as [In],
- CAST(ExitDATETIME AS Time) As Out,
- RIGHT('0' + CAST(Seconds / 3600 AS VARCHAR(2)), 2) + ':'
- + RIGHT('0' + CAST(Seconds % 3600 / 60 AS VARCHAR(2)), 2) + ':'
- + RIGHT('0' + CAST(Seconds % 3600 % 60 AS VARCHAR(2)), 2)
- AS TotalHours
- FROM Level5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement