0

我想从我的数据库中删除重复的行,这些行共享一些相同的列,但不是所有的列。到目前为止我的代码:

SELECT sfb_id, prs_id_201304, prs_id_201204, vorname, nachname, sex, gebdat, strasse, hausnummer, ort, plz, beg_dat, end_dat, quelle
INTO #duplicates
FROM [Recordlinkage].[dbo].[2012]
GROUP BY vorname, nachname, hausnummer, ort, plz
HAVING COUNT(*) > 1

-- delete all rows that are duplicated
DELETE FROM [Recordlinkage].[dbo].[2012]
FROM [Recordlinkage].[dbo].[2012] o INNER JOIN #duplicates d
ON d.vorname= o.vorname and d.nachname=o.nachname and d.hausnummer=o.hausnummer and d.ort=o.ort and d.plz=o.plz

INSERT INTO [Recordlinkage].[dbo].[2012] (vorname, nachname, hausnummer, ort, plz)
SELECT sfb_id, prs_id_201304, prs_id_201204, vorname, nachname, sex, gebdat, strasse, hausnummer, ort, plz, beg_dat, end_dat, quelle
FROM #duplicates

我只想删除在 vorname、nachname、hausnummer、ort und plz 中相同的重复项。我测试它只选择这组变量。它有效,但我错过了之前未选择的所有其他列中的信息。

4

2 回答 2

1

像这样的东西:你可以试试这个

WITH CTE AS
(
SELECT *,RN=ROW_NUMBER() OVER (PARTITION BY columnname ORDER BY columnname DESC) FROM tablename 
)
delete from CTE where RN>1
go
于 2013-11-07T09:39:49.010 回答
0

像这样的东西:

DELETE
FROM (SELECT vorname, nachname, hausnummer, ort, plz,
           ROW_NUMBER()OVER(PARTITION BY vorname, nachname, hausnummer, ort, plz
                            ORDER BY sfb_id) as rnk
           FROM [Recordlinkage].[dbo].[2012]) a
WHERE a.rnk > 1
于 2013-11-07T08:52:40.997 回答