如果小于IN时间,则删除OUT时间值

时间:2016-09-08 10:48:14

标签: sql sql-server

我有下面的表格,并且从我们的系统中得到一些时间戳错误,我想删除时间,因为我的IN / OUT在几秒钟内几乎不同的IN / OUT。

如果OUT时间小于IN且声音不同,则需要删除。

我的查询:

SELECT *
FROM TEST
WHERE Id IN (
        SELECT ca.Id
        FROM test m
        CROSS APPLY (
            SELECT TOP 1 Id
                ,TagID
                ,TIMESTAMP
                ,ZoneCoordinateGroupID
            FROM test tmp
            WHERE tmp.TagID = m.TagID
                --AND tmp.Timestamp > m.Timestamp 
                AND DATEDIFF(SS, TMP.TIMESTAMP, m.TIMESTAMP) > 30
                AND tmp.Id > m.Id
            ORDER BY TIMESTAMP
            ) AS ca
        )

数据为:

ID      TagID           AntennaID   RSSI    Timestamp                   TagData ZoneCoordinateGroupID   ZType DBTimeStamp       BatteryStatus   Longitude   Latitude    Accuracy
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
169343  0.432.076.708   6           -97     2016-09-06 02:36:58.817     1004    OUT                     2016-09-06 06:36:59.320 Good            NULL        NULL        NULL
169347  0.432.076.708   7           -95     2016-09-06 02:37:00.440     1006    IN                      2016-09-06 06:37:01.390 Good            NULL        NULL        NULL
169714  0.432.076.708   8           -92     2016-09-06 03:57:31.713     1004    OUT                     2016-09-06 07:57:33.077 Good            NULL        NULL        NULL

0 个答案:

没有答案