0

I have a table that has 2 columns, SSN and StateID. There are duplicate rows with the same SSN, but not all rows have a StateID. I want to select the rows that have a null StateID, and update it with the row with a matching SSN, and has a StateID.

Example of data

SSN           StateID 
123456789      XYZ 
123456789 
123456789
000000001      ABC 
000000001 

I want to update the null values in StateID with the row value that does not have a null value.

1
  • 2
    Tag your question with the database that you are using. Commented Sep 9, 2019 at 1:12

1 Answer 1

1

First, you should fix your data model. It is a bad idea to replicate data across multiple rows. You should have a table with row per state and SSN and then use a join to connect them.

That said, in most databases you can do:

update t
    set stateid = (select max(t2.stateid) from t t2 where t2.ssn = t.ssn)
    where t.stateid is null;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.