0

I have table with

id name 
1  aa
1  bb
2  ff
3  df 

I want to check if value is repeated in column "id" then assign the "name" value to next matching ids row Expected:

id name
1   aa
1   aa
2   ff
3   df 

Can any one suggest how this can be achieved?

  • @user187664 - are you saying you want name value on each row to be the min(name) value for each id? – Scott Hodgin Aug 14 at 11:16
0
WITH cte AS (SELECT *, 
                    MIN(name) OVER (PARTITION BY id 
                                    ORDER BY name ASC) rn 
             FROM test)
UPDATE cte 
SET name=rn

fiddle

This will work on the basis of the "lower" letters (IE 'A') will overwrite "higher" letters (IE 'B'), as you have not indicated any preference for how to handle this ordering.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.