I need an update statement to resolve some issues with duplicates in a table on SQL Server 2000. The table structure is shown below. There is no primary key in this table. I need a SQL statement that will update the values in cases where there are duplicates by adding 5 to the value until there are no more duplicates.
DocNumber SeQNumber
Doc001 900
Doc001 900
Doc001 900
Doc001 903
Doc001 904
Desired Result
DocNumber SeqNUmber
Doc001 900
Doc001 905
Doc001 910
Doc001 903
Doc001 904
This is what I have tried My latest attempt is shown below. In that example I am just adding the counter, but the +5 is what I actually need.
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
With UpdateData As
(
SELECT DocNumbr,SeqNumbr,
ROW_NUMBER() OVER (ORDER BY [SeqNumbr] DESC) AS RN
FROM RM10101
)
UPDATE RM10101 SET SeqNumbr = (select max(SeqNumbr) from RM10101 where docNumbr = RM10101.DocNumbr and SeqNumbr=RM10101.SeqNumbr) + (@counter)
FROM RM10101
INNER JOIN UpdateData ON RM10101.DocNumbr = UpdateData.DocNumbr
where rn =@counter
SET @counter = @counter + 1
end
end
Any help would be much appreciated.
Thanks,
Bob