Delete record where duplicate in part of raw

前端 未结 2 1046
伪装坚强ぢ
伪装坚强ぢ 2021-01-29 07:49

I need to delete all records in a table where the time between it 1 or 2 minute or the same and must be the same ID but keep the first record

ID            


        
2条回答
  •  不知归路
    2021-01-29 08:11

    For your specific table, please try the code below. It assumes USERID is the "ID" from your original question and CHECKTIME is the "Time". Hope this works for you!

    DECLARE @Seconds INT = 120
    ;WITH Logs AS (
        SELECT
            [USERID] AS ID,
            [CHECKTIME] AS [Time],
            ROW_NUMBER() OVER (PARTITION BY ID ORDER BY [Time]) AS RowNum
        FROM [NEWFP].[dbo].[CHECKINOUT] L
    )
        DELETE L2
        FROM Logs L1
            INNER JOIN Logs L2
                ON L1.ID = L2.ID
                    AND L1.RowNum = L2.RowNum - 1
        WHERE DATEDIFF(SS, L1.[Time], L2.[Time]) < @Seconds
    

提交回复
热议问题