连续重复的数据只取一条,获取历史轨迹数据的sql语句

历史记录,连续的数据如果重复只取一次有效位置数据。


设备表  T_Device


from clipboard



区域表 T_Area


from clipboard



历史位置记录表  T_DeviceMapRecord


from clipboard



WITH datas
AS (SELECT ROW_NUMBER() OVER (ORDER BY ID ASC) AS Step,
           ROW_NUMBER() OVER (ORDER BY ID DESC) AS TopStep,
           zhu.AreaName,
           zhu.DeviceName,
           zhu.Latitude,
           zhu.Longitude,
           zhu.CreateDate
    FROM dbo.T_DeviceMapRecord zhu
    WHERE zhu.DeviceID = 1)
SELECT *
FROM datas zhu
WHERE zhu.TopStep <= 20
      AND NOT EXISTS
(
    SELECT 1
    FROM datas zi
    WHERE zhu.Latitude = zi.Latitude
          AND zhu.Longitude = zi.Longitude
          AND zhu.Step - zi.Step = 1
          AND zi.TopStep <= 20
);

玩咖指针 2020-03-20 22:53:19