I created a table with the following fields:
Record:
Id int Primary Key, Auto Increment
ForeignId int
IsDuplicateRecord bit NULL
Then I inserted some data:
INSERT INTO Record (ForeignId)
VALUES (5), (5), (1), (2), (3)
After that, I ran the following update statement, (found at http://archive.msdn.microsoft.com/SQLExamples/Wiki/View.aspx?title=DuplicateRows ):
UPDATE Record
SET IsDuplicateRecord = 1
WHERE Id IN (
SELECT MAX(Id)
FROM Record
GROUP BY ForeignId
HAVING COUNT(*) > 1
)
So far so good, the query affected one row, and the table now looks like this:
Id ForeignId IsDuplicateRecord
0 5 NULL
1 5 1
2 1 NULL
3 2 NULL
4 3 NULL
I was happy, because for a moment I thought everything was going to be just fine. But then a suspicion as dark as the clouds outside crossed my mind: Dreadingly, I typed
INSERT INTO Record (ForeignId)
VALUES (1), (1)
and ran the above query again, which this time yielded:
Id ForeignId IsDuplicateRecord
0 0 NULL
1 5 1
2 1 NULL
3 2 NULL
4 3 NULL
5 1 NULL
6 1 1
So I figured I'd head over to StackOverflow, and see who could explain to me why the IsDuplicatedRecord field in row with ID 5 wasn't updated to 1? Are you the one?