1

I'm trying to update a table based on a match to another table for multiple columns. I've tried what is shown below but I'm getting the error shown. How is this done?

update my_table set flag = '1' where (patient_id, org) in (
  select distinct (patient_id, org) from enc where lower(enc_type) like '%visit%'
)

Error:

Error in SQL statement: AnalysisException: IN/EXISTS predicate sub-queries can only be used in Filter/Join and a few commands: 'DeltaUpdateTable [post_partum#1911], [1], named_struct

--- EDIT------------------------------

The following is a full example of updating a table based on a complex query that worked based upon the documentation given in the accepted answer.

MERGE INTO events eve
USING (
  select 
    enc.org as org,
    enc.person_id as person_id,
    min(encounter_date) as visit_day
  from 
    enc
    join events eve on enc.org = eve.org and enc.person_id = eve.person_id and eve.is_post_partum = 1
  where 
    lower(enc.enc_type) like '%visit%'
  group by 1, 2
) visits
ON eve.org = visits.org and eve.person_id = visits.person_id
WHEN MATCHED THEN
  UPDATE SET eve.delivery_date = visits.visit_day
;

1 Answer 1

2

First off, make sure you are using Delta Lake as the table format. Second, I think you are looking for Upserts, which are defined as

An operation that inserts rows into a database table if they do not already exist, or updates them if they do.

To do so you'll need to use MERGE combined with UPDATE. Here's an example with the matching expression:

MERGE INTO events
USING updates
ON events.eventId = updates.eventId
WHEN MATCHED THEN
  UPDATE SET events.data = updates.data
WHEN NOT MATCHED
  THEN INSERT (date, eventId, data) VALUES (date, eventId, data)

See more in the Databricks documentation here.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I was able to do what I needed to based on the documentation link you provided.

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.