Skip to content
Advertisement

Calculate time difference between two records and insert into another table if difference is greater than 30 seconds

I have the following table

Tran_DeviceAttRec table:

I want to select and insert only those fields which time difference is more than 30 second into temp table for example if emp_id 2 punchdate is 2021-02-01 16:52:48.000 and Dev_direction is IN and the same emp_id 2 punchdate is 2021-02-01 16:52:54.000 and Dev_direction is OUT then it should not select and insert value to temp table

TempTable:

I am using SQL Server 2014. I tried following queries to find the time but I don’t know how to calculate the difference between time

Advertisement

Answer

I want to select and insert only those fields which time difference is more than 30 second into temp table for example

Do you just want lag()?

Your code and some of the explanation mention other conditions. To be honest, I don’t follow them and this seems to produce what you want.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement