简体   繁体   English

找时间戳之间的差距

[英]Find a gap between time stamp

I have many many rows in the table. 我在表中有很多行。 The table has a Date column (which includes date and time) 该表有一个Date列(包括日期和时间)

I want to be able to loop through the table and find any gap between the two rows where the difference between the row is 5 min. 我希望能够遍历表格并找到两行之间的任何间隙,其中行之间的差异为5分钟。

For example: 例如:

ID Date
1 2014-07-29 13:00:00
2 2014-07-29 13:01:00
3 2014-07-29 13:07:00

So as you can see the time difference between the first row and second row is 1 min so I ignore, then I should be checking the time between the second row and third row. 所以你可以看到第一行和第二行之间的时差是1分钟,所以我忽略了,那么我应该检查第二行和第三行之间的时间。 Since the time difference is 6 min I want to spit out an exception with the dates that were compared. 由于时差是6分钟,我想用比较的日期吐出一个例外。

The table could contain many rows, so I would go and check the next record to the previous one and so one... 该表可能包含很多行,所以我会去查看前一个记录的下一个记录,所以一个......

How could I achieve this in SQL Server. 我怎样才能在SQL Server中实现这一点。 I can do a datediff, but I will have a lot of rows and I don't want to perform this manually. 我可以做一个约会,但我会有很多行,我不想手动执行。

Any suggestions? 有什么建议么?

NOTE* I don't need to worry about the cross over of hours from one day to another, since this task is only going to be used for a single day. 注意*我不需要担心从一天到另一天的交叉时间,因为这项任务仅用于一天。 I will specify on SQL statement where date = getdate() 我将在SQL语句中指定date = getdate()

One way to do it 一种方法

WITH ordered AS
(
  SELECT *, ROW_NUMBER() OVER (ORDER BY date) rn
    FROM table1 
)
SELECT o1.id id1, o1.date date1, o2.id id2, o2.date date2, DATEDIFF(s, o1.date, o2.date) diff
  FROM ordered o1 JOIN ordered o2
    ON o1.rn + 1 = o2.rn 
 WHERE DATEDIFF(s, o1.date, o2.date) > 60

Output: 输出:

| ID1 |                       DATE1 | ID2 |                       DATE2 | DIFF |
|-----|-----------------------------|-----|-----------------------------|------|
|   2 | July, 29 2014 13:01:00+0000 |   3 | July, 29 2014 13:07:00+0000 |  360 |

Here is SQLFiddle demo 这是SQLFiddle演示

Since you are on SQL Server 2012, you can make use of the LEAD and LAG functions, like so: 由于您使用的是SQL Server 2012,因此您可以使用LEADLAG函数,如下所示:

;with cte as
(select id, 
 [date], 
 datediff(minute,[date],lead([date],1,0) over (order by [date])) difference,
 row_number() over (order by [date]) rn
from tbl)

select * from cte
where rn <> 1 --Skip first row ordered by the date
and difference > 5

This will return all rows which have a difference of more than 5 minutes with the next row. 这将返回与下一行相差5分钟以上的所有行。 The assumption is that the rows are sorted in ascending order of date. 假设行按日期的升序排序。

Demo 演示

If you can leverage the ID column being ascending and incrementing by one: 如果您可以利用ID列升序并递增1:

select a.id, b.id, a.date, b.date
  from mytable a
  join mytable b
    on b.id = a.id - 1
 where datediff(minute, a.date, b.date) < 5

If you cannot leverage that: 如果你不能利用它:

with x as (
select row_number() over(order by date) as rownum, date
  from mytable
)
select a.rownum, b.rownum, a.date, b.date
  from x a
  join x b
    on b.rownum = a.rownum - 1
 where datediff(minute, a.date, b.date) < 5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM