Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 delete duplicates

Author  Topic 

pingili07656
Starting Member

3 Posts

Posted - 2012-08-09 : 16:13:53
Hi all i have a table with duplicate values like below

2010-06-30, 00:00:00.000, 2010-06-30 00:00:00.000, 2010-06-30 23:44:00.000, 2010-07-02 12:09:00.000 , NULL, 312734,495789, , 52i523

AND
2010-06-30, 00:00:00.000, 2010-06-30 00:00:00.000, 2010-06-30 23:44:00.000, 2010-07-02 12:09:00.000 , NULL, 312734,495789,NULL , 52i523

I would like to have only the below record in my table
2010-06-30, 00:00:00.000, 2010-06-30 00:00:00.000, 2010-06-30 23:44:00.000, 2010-07-02 12:09:00.000 , NULL, 312734,495789, , 52i523


Any help is appriciated. Thanks

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-09 : 16:17:37
[code]
SELECT field1,field2,....
FROM
(
SELECT ROW_NUMBER() OVER (PARTITION BY field1,field2,... ORDER BY PK) AS Seq,
*
FROM table
)t
WHERE Seq=1
[/code]

field1,field2,... are set columns with same value for multiple rows and pk is primary key

once you do above and confirm that you get correct unique records then convert select logic to delete to remove duplicate entries

[code]
DELETE t
FROM
(
SELECT ROW_NUMBER() OVER (PARTITION BY field1,field2,... ORDER BY PK) AS Seq
FROM table
)t
WHERE Seq>1
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -