How to make a sql loop query to check difference between values?

前端 未结 1 623
挽巷
挽巷 2021-01-26 09:52

first of all, I\'m not a native english speaker and this is a hard to explain issue. If you have any doubts, please let me know.

We\'re using a GPS vehicle tracking devi

相关标签:
1条回答
  • 2021-01-26 10:13
    DECLARE @idFrom as int,
            @idTo as int,
            @gpsDateFrom as datetime,
            @gpsDateTo as datetime
    DECLARE VehicleCursor CURSOR FAST_FORWARD FOR 
    SELECT  vehicle_gps_id, 
            datetimeCol
    FROM    yourtable
    ORDER BY vehicle_gps_id
    OPEN VehicleCursor FETCH NEXT FROM VehicleCursor INTO @idFrom, @gpsDateFrom
        FETCH NEXT FROM VehicleCursor INTO @idTo, @gpsDateTo
        WHILE @@FETCH_STATUS = 0 BEGIN 
            IF DATEDIFF(MI,@gpsDateFrom,@gpsDateTo) >5
            BEGIN
                --Break (your code here)
            END
            SET @idFrom = @idTo
            SET @gpsDateFrom = @gpsDateTo
            FETCH NEXT FROM VehicleCursor INTO @idTo, @gpsDateTo
        END 
    CLOSE VehicleCursor 
    DEALLOCATE VehicleCursor
    

    Something like this should work for you. It is a cursor that just runs through all your columns comparing datetimes. You can enter in whatever you want to do into the commented section after the if statement.

    0 讨论(0)
提交回复
热议问题