Problem deleting duplicates using a CTE when there are table joins
I've used CTEs to remove duplicates successfully before, but this time round I wanted to delete them from one table having joined to other tables and it comes back with the error:
View or function 'dupnotesCTE' is not updatable because the modification affects multiple base tables. See the attached code for my sql statement.
Not sure where I've gone wrong here...can someone help please?
;WITH dupnotesCTE([FileNo] ,[CommentDate] ,[CommentTime] ,[CommentUser] ,[Cat] ,[Pri] ,[Line] ,[Note], Ranking)AS(SELECT p.[FileNo] ,p.[CommentDate] ,p.[CommentTime] ,p.[CommentUser] ,p.[Cat] ,p.[Pri] ,p.[Line] ,p.[Note] ,Ranking = row_number() OVER(PARTITION BY p.[FileNo] ,p.[CommentDate] ,p.[CommentTime] ,p.[CommentUser] ,p.[Cat] ,p.[Pri] ,p.[Line] ,p.[Note] ORDER BY p.[FileNo] ,p.[CommentDate] ,p.[CommentTime] ,p.[CommentUser] ,p.[Cat] ,p.[Pri] ,p.[Line] ,p.[Note])FROM filenotes pjoin detailtable d on ltrim(rtrim(p.FileNo)) = ltrim(rtrim(d.FileNo))join country c on d.country_no = c.country_nowhere c.continent in ('Asia','Europe'))delete from dupnotesCTEwhere ranking > 1
delete p
FROM filenotes p
JOIN dupnotesCTE c ON c.FileNo = p.fileNo --??
where c.ranking > 1
Martin Griffiths
ASKER
This won't work though will it as, say you have a pair of duplicates A and B. You have A in dupnotesCTE with rank 1, and B in dupnotesCTE with rank 2. When you link from filenotes to dupnotes you'll have 4 records:
filenotes.A and dupnotes.A (rank 1)
filenotes.A and dupnotes.B (rank 2)
filenotes.B and dupnotes.A (rank 1)
filenotes.B and dupnotes.B (rank 2)
Your "where c.ranking > 1" will remove the 2nd and 4th combinations but you're left with two records i.e. combinations 1 and 3 above.
May be mistaken, but my query seems to reflect this.
Erick37
Does filenotes table have an identity column or primary key that you could use to join the cte?
Having an identity column would greatly simplify identifying the duplicates.
One possibility would be to copy the records over to a temp table with an identity column.
Remove the duplicates from the temp table.
Delete all the records from the user table that you originally copied to the temp table.
Insert the cleaned records back into filenotes table.
...
delete p
FROM filenotes p
JOIN dupnotesCTE c ON c.FileNo = p.fileNo --??
where c.ranking > 1