Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete duplicate data from table

Status
Not open for further replies.

bmacbmac

IS-IT--Management
Jan 26, 2006
392
US
Hello,

I have the following table:

Code:
create table #temp (relatedid int, recordid int, docno varchar(20), bookno varchar(20), pageno varchar(20))
insert into #temp values (1, 89, '12345', '100', '444')
insert into #temp values (2, 89, '12345', '100', '444')
insert into #temp values (3, 89, '12345', '100', '444')
insert into #temp values (4, 90, '55555', '200', '555')
insert into #temp values (5, 90, '55555', '200', '555')
insert into #temp values (6, 91, '77777', '300', '888')
insert into #temp values (7, 91, '77777', '300', '888')
insert into #temp values (8, 91, '77777', '300', '888')
insert into #temp values (9, 91, '77777', '300', '888')


I am looking for a way to remove the duplicate recordid, docno, bookno, pageno values. I don't care which one stays, but I just want one of each instance to stay. So results like:

relatedid, recordid, docno, bookno, pageno
1, 89, '12345', '100', '444'
4, 90, '55555', '200', '555'
6, 91, '77777', '300', '888'


I am writing something that is just turning into a whole bunch of #temp tables and turning into kind of a mess. I think I am making it way to hard.. there has to be a better solution out there. Any ideas?

Thanks!
 
on this particular case this will do the trick.

assumption, based on the supplied data, is that relatedid is unique

Code:
delete t
from #temp t
left outer join (select  min(relatedid) as relatedid
from #temp
group by recordid
       , docno
       , bookno
       , pageno
) t1
on t.relatedid = t1.relatedid
where t1.relatedid is null

Regards

Frederico Fonseca
SysSoft Integrated Ltd

FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top