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
 deleting duplicate rows in table

Author  Topic 

cognos
Starting Member

10 Posts

Posted - 2011-06-16 : 04:19:01
i am deleting duplicate rows in table

delete from Test A where A.rowid >
(select min(B.rowid) from Test B where A.id = B.id)

DELETE FROM Test
WHERE rowid NOT IN
(SELECT max(rowid) FROM Test
GROUP BY id
HAVING count(*)>=1)
when i was executing these first one Incorrect syntax near 'A' error coming and executing second one also row id error coming.please can you any one help me in this


ddd

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-06-16 : 04:20:53
Are you on SQL Server 2005 or above?


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

cognos
Starting Member

10 Posts

Posted - 2011-06-16 : 05:03:16
sqlserver 2008

ddd
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-06-16 : 05:29:53
Test this please:

delete dt
from
(
select
row_number() over (partition by id order by rowid asc) as rownum,
*
)dt
where rownum > 1

If it is not working then please give table structure, sample data and wanted result.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

cognos
Starting Member

10 Posts

Posted - 2011-06-16 : 05:37:14
id name city
1 john usa
1 john usa
2 peter uk

if table is like this i want to delete dupicate rows means 1 john usa
1 john usa how can do this?

ddd
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-06-16 : 05:40:14
delete dt
from
(
select
row_number() over (partition by id order by name asc) as rownum,
*
from HereComesYourTableName
)dt
where rownum > 1


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -