簡體   English   中英

計算兩條記錄之間的時間差,如果差值大於 30 秒,則插入另一個表

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

我有下表

Tran_DeviceAttRec 表:

Emp_id  Card_Number Dev_Id  Dev_Direction   Punch_RawDate           sno_id    
    1       1           1   IN              2021-02-01 16:52:26.000 331
    2       2           1   IN              2021-02-01 16:52:48.000 332
    2       2           2   OUT             2021-02-01 16:52:54.000 333
    3       3           1   IN              2021-02-01 16:58:01.000 334
    4       4           1   IN              2021-02-01 16:58:46.000 335
    3       3           2   OUT             2021-02-01 16:59:02.000 336
    4       4           2   OUT             2021-02-01 18:25:00.000 338
    1       1           2   OUT             2021-02-01 18:26:00.000 339

我想 select 並僅將時差超過 30 秒的字段插入到臨時表中,例如如果 emp_id 2 punchdate 是 2021-02-01 16:52:48.000 並且 Dev_direction 是 IN 並且相同的 emp_id 2 punchdate 是 2021- 02-01 16:52:54.000 和 Dev_direction 是 OUT 那么它不應該 select 並將值插入臨時表

臨時表:

Emp_id  Card_Number Dev_Id  Dev_Direction   Punch_RawDate           sno_id
1       1           2       OUT             2021-02-01 18:26:00.000 339
3       3           2       OUT             2021-02-01 16:59:02.000 336
4       4           2       OUT             2021-02-01 18:25:00.000 338

我正在使用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

例如,我想 select 並僅將時差超過 30 秒的字段插入臨時表

你只想要lag()嗎?

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'  

使用 self join 和 datediff function 得到結果

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM