Find and Delete duplicates in SMS and SCCM db

SMS 2.0 had problems with duplicates in the database and this problem was not solved in SMS2003. Now I have seen it also in SCCM.

I have been using these two below scripts many times to manually find and delete all duplicates only keeping the latest record.

First script shows all duplicates:

SELECT DIS.*
FROM System_DISC DIS
Join ( Select Name0, resource_domain_or_workgr0
FROM System_DISC
group by Name0, resource_domain_or_workgr0
having count(*) > 1)
As DUP on DUP.Name0 = DIS.Name0
And DUP.resource_domain_or_workgr0 = DIS.resource_domain_or_workgr0
order by Netbios_Name0

Second script deletes the duplicates but keep the most recently discovered record:

delete from system_disc where
itemkey in
(SELECT distinct DIS.itemkey
FROM System_DISC DIS
Join ( Select Name0, Creation_Date0
FROM System_DISC where Name0 IN ( Select Name0
FROM System_DISC
group by Name0
having count(*) > 1))
As DUP on DUP.Name0 = DIS.Name0
where (DIS.Creation_Date0 < DUP.Creation_Date0 or DIS.Creation_Date0 is null))