Sql 计算两条记录之间的时间差,如果差值大于30秒,则插入另一个表

Sql 计算两条记录之间的时间差,如果差值大于30秒,则插入另一个表,sql,sql-server,tsql,Sql,Sql Server,Tsql,我有下表 传输设备记录表: 我只想选择那些时差超过30秒的字段并将其插入临时表 例如,如果emp_id 2 punchdate为2021-02-01 16:52:48.000且开发方向为 同一emp_id 2 punchdate为2021-02-01 16:52:54.000,且Dev_方向为OUT,则不应选择并将值插入临时表 诱人的: 我正在使用SQL Server 2014。我试着按照下面的查询查找时间,但我不知道如何计算时间之间的差异 SELECT TOP 1 Emp_id, Punch_

我有下表

传输设备记录表:

我只想选择那些时差超过30秒的字段并将其插入临时表 例如,如果emp_id 2 punchdate为2021-02-01 16:52:48.000且开发方向为 同一emp_id 2 punchdate为2021-02-01 16:52:54.000,且Dev_方向为OUT,则不应选择并将值插入临时表

诱人的:

我正在使用SQL Server 2014。我试着按照下面的查询查找时间,但我不知道如何计算时间之间的差异

SELECT TOP 1 Emp_id, Punch_RawDate
FROM Tran_DeviceAttRec
where Dev_Direction = 'OUT'
ORDER BY sno_id DESC

SELECT TOP 1 Emp_id, Punch_RawDate
from Tran_DeviceAttRec
where Dev_Direction = 'IN' and Emp_id = 1
order by sno_id desc
例如,我只想选择那些时差超过30秒的字段并将其插入临时表

你只是想要滞后吗

您的代码和一些解释提到了其他条件。老实说,我没有遵循它们,这似乎能产生您想要的结果。

请尝试下面的查询

  Create table #table1( Emp_id  int, Card_Number int,  Dev_Id  int,  Dev_Direction varchar(20), 
  Punch_RawDate  datetime,         sno_id    int)

insert into #table1
select  1 ,     1   ,        1 , 'IN'    ,          '2021-02-01 16:52:26.000','331' union all
select  2 ,     2   ,        1 , 'IN'    ,          '2021-02-01 16:52:48.000','332' union all
select  2 ,     2   ,        2 , 'OUT'   ,          '2021-02-01 16:52:54.000','333' union all
select  3 ,     3   ,        1 , 'IN'    ,          '2021-02-01 16:58:01.000','334' union all
select  4 ,     4   ,        1 , 'IN'    ,          '2021-02-01 16:58:46.000','335' union all
select  3 ,     3   ,        2 , 'OUT'   ,          '2021-02-01 16:59:02.000','336' union all
select  4 ,     4   ,        2 , 'OUT'   ,          '2021-02-01 18:25:00.000','338' union all
select  1 ,     1   ,        2 , 'OUT'   ,          '2021-02-01 18:26:00.000','339'  
使用self-join和datediff函数得到结果

select b.* from #table1 a
join #table1 b on a.Emp_id=b.Emp_id and a.Dev_Direction='In' and b.Dev_Direction ='out'
and DATEDIFF(SECOND, a.Punch_RawDate, b.Punch_RawDate)>30
order by b.Emp_id

你考虑过滞后吗?没有,因为我不必和前一排比较。当您说当前行与同一员工的前一行比较的时差超过30秒时,前一行可能有其他员工数据?这正是你使用滞后的原因。
select dar.*
from (select dar.*,
             lag(Punch_RawDate) over (partition by emp_id order by Punch_RawDate) as prev_Punch_RawDate
      from Tran_DeviceAttRec dar
     ) dar
where dar.Punch_RawDate > dateadd(second, 30, prev_Punch_RawDate);
  Create table #table1( Emp_id  int, Card_Number int,  Dev_Id  int,  Dev_Direction varchar(20), 
  Punch_RawDate  datetime,         sno_id    int)

insert into #table1
select  1 ,     1   ,        1 , 'IN'    ,          '2021-02-01 16:52:26.000','331' union all
select  2 ,     2   ,        1 , 'IN'    ,          '2021-02-01 16:52:48.000','332' union all
select  2 ,     2   ,        2 , 'OUT'   ,          '2021-02-01 16:52:54.000','333' union all
select  3 ,     3   ,        1 , 'IN'    ,          '2021-02-01 16:58:01.000','334' union all
select  4 ,     4   ,        1 , 'IN'    ,          '2021-02-01 16:58:46.000','335' union all
select  3 ,     3   ,        2 , 'OUT'   ,          '2021-02-01 16:59:02.000','336' union all
select  4 ,     4   ,        2 , 'OUT'   ,          '2021-02-01 18:25:00.000','338' union all
select  1 ,     1   ,        2 , 'OUT'   ,          '2021-02-01 18:26:00.000','339'  
select b.* from #table1 a
join #table1 b on a.Emp_id=b.Emp_id and a.Dev_Direction='In' and b.Dev_Direction ='out'
and DATEDIFF(SECOND, a.Punch_RawDate, b.Punch_RawDate)>30
order by b.Emp_id