This is my second attempt at this. I am trying to update data in a SQL table where there are duplicates to resolve the duplicate.
Data Structure
DocNumbr SeQnumbr Dex_Row_Id
CRM025222 32768 1
CRM025222 32768 2
CRM025222 16384 3
Desired Result
DocNumbr SeQnumbr Dex_Row_Id
CRM025222 49152 1
CRM025222 32768 2
CRM025222 16384 3
For each duplicate, I need to update the seqnumbr to be the max value of seqnumbr for the duplicates by Docnumbr + 16384. Dex_row_ID is a primary key and is unique.
My Attempt
Begin
Declare @count as integer = (SELECT COUNT(*) AS DUPLICATES
FROM dbo.RM10101
GROUP BY DocNumbr, SeqNumbr
HAVING (COUNT(*) > 1))
Declare @counter as integer =1
While @Counter < @count
begin
Update RM10101 set SeqNumbr= (select MAX(dex_row_id) FROM RM10101 E2
WHERE E2.DocNumbr = RM10101.DocNumbr AND E2.SeqNumbr = RM10101.SeqNumbr) + (16384+)
WHERE dex_row_id < (select MAX(dex_row_id) FROM RM10101 E2
WHERE E2.DocNumbr = RM10101.DocNumbr AND E2.SeqNumbr = RM10101.SeqNumbr
)
SET @counter = @counter + 1
END
end
I have tried the code above, but the value being used to add to 16384 is the max seqnumbr of all rows, I need it be the max for the set of duplicates. I am not a dba, but am being pressed into duty on this and am really lost on how to get the join or subquery right. This is being done in SQL Server 2008. Originally I was told it was 2000, so if there is a better way using a cte or some other new method, I would be open to it. Any help would be much appreciated. Thanks, Bob