Remove duplicate records except the first record in SQL

后端 未结 3 2037
情话喂你
情话喂你 2020-12-22 05:36

I want to remove all duplicate records except the first one.

Like :

NAME
R
R
rajesh
YOGESH
YOGESH

Now in the above I want to remove

3条回答
  •  感情败类
    2020-12-22 05:51

    This is bigger code but it works perfectly where you don't take the original row but find all the duplicate Rows

        select majorTable.RowID,majorTable.Name,majorTable.Value from 
        (select outerTable.Name, outerTable.Value, RowID, ROW_NUMBER() 
    over(partition by outerTable.Name,outerTable.Value order by RowID)
         as RowNo from @Your_Table outerTable inner join
        (select Name, Value,COUNT(*) as duplicateRows  FROM @Your_Table group by Name, Value 
    having COUNT(*)>1)innerTable on innerTable.Name = outerTable.Name 
        and innerTable.Value = outerTable.Value)majorTable where MajorTable.ROwNo <>1
    

提交回复
热议问题