How can I delete the duplicate rows while keeping the original record?

时间:2016-10-19 13:38:36

标签: sql-server tsql

I have an one table as below a picture which indicates some duplicated rows.I can find the duplicated rows but I could not able to delete it because of there is no any unique ID that I can distinguish. There were lots of duplicated rows like that in same table I just screenshot a piece of that.

As a result,according to the below picture, how can I delete the duplicated rows but keep original ?

enter image description here

4 个答案:

答案 0 :(得分:0)

您可以考虑的一个解决方案是将所有唯一记录复制到临时表中,从而删除重复项。然后,您可以截断原始表并从您创建的临时表中重新填充它。代码将是这样的:

SELECT DISTINCT * INTO #tempTable FROM MyTable

TRUNCATE TABLE MyTable;
INSERT INTO MyTable (LocationID, UnitID, CameraID ... IsActiveHours) 
SELECT LocationID, UnitID, CameraID ... IsActiveHours FROM #tempTable;

由于关键约束和数据量,这并不总是一个选项,但在某些情况下很有用。你可以随心所欲。

答案 1 :(得分:0)

您可以使用cte和Row_Number()来完成此任务。如果您对结果感到满意,请删除最终选择并取消注释删除语句

;with cte as (
    Select *,RowNr=Row_Number() over (Partition By LocationId Order by Date_T) 
     From   YourTable
)
Select * from cte Where RowNr>1
-- Delete From cte Where RowNr>1   

答案 2 :(得分:0)

这是解决此问题的查询。

    WITH X AS (
    SELECT ROW_NUMBER() OVER(PARTITION BY  LocationId,date_t ORDER BY LocationId  desc) as 'rownum',LocationId,
    date_T AS T
    FROM Counts
    )
    --SELECT * FROM X WHERE rownum >1 

DELETE FROM X
WHERE rownum <> 1

答案 3 :(得分:0)

您最好添加一个标识列以使事情变得更容易,但是这可以在没有使用以下内容的TRUNCATE的情况下完成:

    --GET DUPLICATE ROWS INTO A TEMP TABLE (YOU MAY NOT NEED TO USE ALL THE COLUMNS TO IDENTIFY A DUPLICATE)
    SELECT ROW_NUMBER() OVER (ORDER BY ColA) AS RowNo, ColA, ColB, ColC, COUNT(*) As [Count] 
    INTO #TEMP1
    FROM test 
    GROUP BY ColA, ColB, ColC
    HAVING COUNT(*) > 1

    --LOOP THROUGH DUPLICATES
    DECLARE @RowNo INT
    DECLARE @Duplicates INT

    SET @RowNo = 1

    WHILE EXISTS(SELECT * FROM #TEMP1)
    BEGIN

        --GET A COUNT OF ADDITIONAL ROWS FOR THIS DUPLICATE
        SET @Duplicates = (SELECT [Count] FROM #TEMP1 WHERE RowNo = @RowNo) - 1

        --DELETE THE ROWS WE DONT NEED
        DELETE TOP (@Duplicates) t1
        FROM test t1
        JOIN #TEMP1 t2 ON t1.ColA = t2.ColA AND t1.ColB = t2.ColB AND t1.ColC = t2.ColC
        WHERE t2.RowNo = @RowNo

        --REMOVE THE ROW FROM THE TEMP TABLE
        DELETE FROM #TEMP1 WHERE RowNo = @RowNo

        --INCREASE THE ROW NO TO MOVE TO THE NEXT ROW
        SET @RowNo = @RowNo + 1
    END

    --DROP THE TEMP TABLE
    DROP TABLE #TEMP1