在SQL Server中过滤日期时间列

时间:2018-07-03 14:29:30

标签: sql-server tsql datetime sql-server-2014

我有一个datetime列,下一个数据之间的间隔为5分钟,但是,我想查看该列是否包含少于5分钟(特别是5秒)的任何​​时间间隔。

例如:

  • 一个日期将显示为2018-05-04 19:21:46.000
  • 下一行将显示为2018-05-04 19:26:46.000
  • 2018-05-04 19:31:46.000

但是,有时我们会得到以下行:

  • 2018-05-04 19:36:46.000
  • 然后2018-05-04 19:36:51.000
  • 然后2018-05-04 19:36:56.000

哪种SQL脚本最适合过滤列以区分错误数据(5秒间隔)和正确数据(5分钟间隔),尤其是在具有数千行的表中?

您好,@ Andrea,谢谢。我有一些问题。 “ q”代表什么?当我将查询重写为

SELECT  ProductID, MyTimestamp, DATEDIFF(second, xMyTimestamp, MyTimestamp) as DIFFERENCE_IN_SECONDS
FROM    (
        SELECT  *,
                Lag(MyTimestamp) OVER (ORDER BY MyTimestamp, ProductID) as xMyTimestamp
        FROM    TableName
        ) q
WHERE   xMyTimestamp IS NOT NULL and ProductID= 31928

我得到的结果无法准确计算时间。

+-----------+-------------------------+-----------------------+
| ProductID |       MyTimestamp       | DIFFERENCE_IN_SECONDS |
+-----------+-------------------------+-----------------------+
|     31928 | 2017-03-21 13:36:30.000 |                     0 |
|     31928 | 2017-03-21 13:46:30.000 |                     0 |
|     31928 | 2017-03-21 13:56:32.000 |                     0 |
|     31928 | 2017-03-21 14:01:32.000 |                     0 |
|     31928 | 2017-03-21 14:11:32.000 |                     0 |
|     31928 | 2017-03-21 14:16:32.000 |                     0 |
|     31928 | 2017-03-21 14:26:32.000 |                     0 |
|     31928 | 2017-03-21 14:36:32.000 |                     0 |
+-----------+-------------------------+-----------------------+

任何理由

2 个答案:

答案 0 :(得分:1)

自2014年起,您可以使用LEAD将一行的值与下一行的值进行比较。

declare @table table(id int identity(1,1), interval datetime)
insert into @table
values
('2018-05-04 19:21:46.000'),
('2018-05-04 19:26:46.000'),
('2018-05-04 19:31:46.000'),

('2018-05-04 19:36:46.000'),
('2018-05-04 19:36:51.000'),
('2018-05-04 19:36:56.000')

select
    id
    ,interval
    ,issue_with_row = case 
                            when 
                                isnull(datediff(minute,interval,lead(interval) over (order by id, interval)),0) < 5 
                            then 1 
                            else 0 
                        end
from @table
order by id

或者,如果您只想看那些,

;with cte as(
select
    id
    ,interval
    ,issue_with_row = case 
                            when 
                                isnull(datediff(minute,interval,lead(interval) over (order by id, interval)),0) < 5 
                            then 1 
                            else 0 
                        end
from @table)

select *
from cte 
where issue_with_row = 1

答案 1 :(得分:0)

您可以使用LAG

declare @tmp table(MyTimestamp datetime)

insert into @tmp values
('2018-05-04 19:21:46.000')
,('2018-05-04 19:26:46.000')
,('2018-05-04 19:31:46.000')
,('2018-05-04 19:36:46.000')
,('2018-05-04 19:36:51.000')
,('2018-05-04 19:36:56.000')


SELECT  DATEDIFF(second, xMyTimestamp, MyTimestamp) as DIFFERENCE_IN_SECONDS
FROM    (
        SELECT  *,
                LAG(MyTimestamp) OVER (ORDER BY MyTimestamp) xMyTimestamp
        FROM    @tmp
        ) q
WHERE   xMyTimestamp IS NOT NULL

结果:

enter image description here

所以您应该像这样使用它:

SELECT  DATEDIFF(second, xMyTimestamp, MyTimestamp) as DIFFERENCE_IN_SECONDS
FROM    (
        SELECT  *,
                LAG(MyTimestamp) OVER (ORDER BY MyTimestamp) xMyTimestamp
        FROM    [YOUR_TABLE_NAME_HERE]
        ) q
WHERE   xMyTimestamp IS NOT NULL

编辑

这是基于OP发布的新数据的另一个示例:

declare @tmp table(ProductID int, MyTimestamp datetime)

insert into @tmp values
 (31928, '2017-03-21 13:36:30.000')
,(31928, '2017-03-21 13:46:30.000')
,(31928, '2017-03-21 13:56:32.000')
,(31928, '2017-03-21 14:01:32.000')
,(31928, '2017-03-21 14:11:32.000')
,(31928, '2017-03-21 14:16:32.000')
,(31928, '2017-03-21 14:26:32.000')
,(31928, '2017-03-21 14:36:32.000')

SELECT ProductID
    ,MyTimestamp
    ,DATEDIFF(second, xMyTimestamp, MyTimestamp) AS DIFFERENCE_IN_SECONDS
FROM (
    SELECT *
        ,Lag(MyTimestamp) OVER (
            ORDER BY MyTimestamp
                ,ProductID
            ) AS xMyTimestamp
    FROM @tmp
    ) q
WHERE xMyTimestamp IS NOT NULL
    AND ProductID = 31928

输出:

enter image description here

Here,您可以检查结果是否计算正确。