SQL – Update Table for Unique ID with Multiple Entries

update

I'm trying to update a table to fill in a variable to each row in the table if one row for the unique ID shows that variable.

I was trying to do the following, but it's giving me an error. Appreciate the help!

update patient_encounter 
Set user_defined4 = 'DTC'
where person_id = (SELECT person_id from patient_encounter where  mrkt_plan_id = '489681B4-9D94-413B-95A0-C50D5964F340')

I want to fill in "DTC" into the user_defined4 field for any person_id that shows the particular mrkt_plan_id listed (which correlates to DTC) for any of their entries. Each person_id has multiple encounters and I want them all to share the same market plan once it is listed as DTC once.

The error says Subquery returned more than 1 value.
Thanks!

Best Answer

There multiple entry in table patient_encounter for a person_id, so you need to get the person_id with top 1

Try this

update patient_encounter 
Set user_defined4 = 'DTC'
where person_id = (SELECT top 1 person_id from patient_encounter 
                     where  mrkt_plan_id = '489681B4-9D94-413B-95A0-C50D5964F340')

EDIT as per comment, if you are looking to update all the record, use IN as below

update patient_encounter 
    Set user_defined4 = 'DTC'
    where person_id IN (SELECT person_id from patient_encounter 
                         where  mrkt_plan_id = '489681B4-9D94-413B-95A0-C50D5964F340')