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
 Dedupe help

Author  Topic 

Yellowdog
Starting Member

34 Posts

Posted - 2009-07-30 : 11:12:31
Hay guys,
I need some help dedupeing a table.

I am adding to a dupe table like so


insert into cash_dupes (full_name,first_name, last_name, address, city, state, zip, year, make, model, flag)
select full_name, first_name, last_name, address, city, state, zip, year, make, model, flag = 1 from cash_data_hold
group by full_name, first_name, last_name, address, city, state, zip, year, make, model having count(*) > 1


then I can find the dupes in the existing table like so


select * from cash_data_hold c inner join cash_dupes d on
d.full_name = c.full_name and
d.first_name = c.first_name and
d.last_name = c.last_name and
d.address = c.address and
d.city = c.city and
d.state = c.state and
d.zip = c.zip and
d.year = c.year and
d.make = c.make and
d.model = c.model


My question is how to delete the max row id in the original table.

The original table has a identity column named record_id and I want to delete the max record_id.

Any help would be greatly appreciated!

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-07-30 : 13:45:28
[code]DELETE t
FROM yourtable t
inner join (select full_name, first_name, last_name, address, city, state, zip, year, make, model,max(record_id) as latest
from yourtable
group by full_name, first_name, last_name, address, city, state, zip, year, make, model) t1
on t1.full_name = t.full_name
and t1.first_name=t.first_name
and t1.last_name=t.last_name
and t1.address=t.address
and t1.city=t.city
and t1.state=t.state
and t1.zip=t.zip
and t1.year=t.year
and t1.make= t.make
and t1.model=t.model
and t1.latest=t.record_id
[/code]
Go to Top of Page
   

- Advertisement -