I am looking to create a query that counts how many times an employee is inactive for 5-minutes. Right now, I have thought about a CASE statement that will break down their scans into 12- segments (don't mind the conversion-- we have to change all of our takes to 4 hours behind because the server is in another timezone (it sucks)
case
when DATEPART(minute, CONVERT(datetime, SWITCHOFFSET(CONVERT(datetimeoffset, th1.ACTIVITY_DATE_TIME), DATENAME(TzOffset, SYSDATETIMEOFFSET())))) between '0' and '4' then 0
when DATEPART(minute, CONVERT(datetime, SWITCHOFFSET(CONVERT(datetimeoffset, th1.ACTIVITY_DATE_TIME), DATENAME(TzOffset, SYSDATETIMEOFFSET())))) between '5' and '9' then 1
when DATEPART(minute, CONVERT(datetime, SWITCHOFFSET(CONVERT(datetimeoffset, th1.ACTIVITY_DATE_TIME), DATENAME(TzOffset, SYSDATETIMEOFFSET())))) between '10' and '14' then 2
when DATEPART(minute, CONVERT(datetime, SWITCHOFFSET(CONVERT(datetimeoffset, th1.ACTIVITY_DATE_TIME), DATENAME(TzOffset, SYSDATETIMEOFFSET())))) between '15' and '19' then 3
when DATEPART(minute, CONVERT(datetime, SWITCHOFFSET(CONVERT(datetimeoffset, th1.ACTIVITY_DATE_TIME), DATENAME(TzOffset, SYSDATETIMEOFFSET())))) between '20' and '24' then 4
when DATEPART(minute, CONVERT(datetime, SWITCHOFFSET(CONVERT(datetimeoffset, th1.ACTIVITY_DATE_TIME), DATENAME(TzOffset, SYSDATETIMEOFFSET())))) between '25' and '29' then 5
when DATEPART(minute, CONVERT(datetime, SWITCHOFFSET(CONVERT(datetimeoffset, th1.ACTIVITY_DATE_TIME), DATENAME(TzOffset, SYSDATETIMEOFFSET())))) between '30' and '34' then 6
when DATEPART(minute, CONVERT(datetime, SWITCHOFFSET(CONVERT(datetimeoffset, th1.ACTIVITY_DATE_TIME), DATENAME(TzOffset, SYSDATETIMEOFFSET())))) between '35' and '39' then 7
when DATEPART(minute, CONVERT(datetime, SWITCHOFFSET(CONVERT(datetimeoffset, th1.ACTIVITY_DATE_TIME), DATENAME(TzOffset, SYSDATETIMEOFFSET())))) between '40' and '44' then 8
when DATEPART(minute, CONVERT(datetime, SWITCHOFFSET(CONVERT(datetimeoffset, th1.ACTIVITY_DATE_TIME), DATENAME(TzOffset, SYSDATETIMEOFFSET())))) between '45' and '49' then 9
when DATEPART(minute, CONVERT(datetime, SWITCHOFFSET(CONVERT(datetimeoffset, th1.ACTIVITY_DATE_TIME), DATENAME(TzOffset, SYSDATETIMEOFFSET())))) between '50' and '54' then 10
when DATEPART(minute, CONVERT(datetime, SWITCHOFFSET(CONVERT(datetimeoffset, th1.ACTIVITY_DATE_TIME), DATENAME(TzOffset, SYSDATETIMEOFFSET())))) between '55' and '59' then 11
END))
I then took the distinct amount of numbers 0-11 and subtracted it from 12. I know this is not a perfect system because if someone has a scan at 7:01 and the next one at 7:09, it will not flag a 5-minute gap.
Does anyone know a better way of tracking gaps in transaction history?
LAGanalytic function comes to your rescue.Here a simple demonstration using this sample data
This query calculate the difference of the current timestamp with the value from the previous row of the same employee in minutes.
It remains only to filter the intervals that are longer than the requested 5 minutes: