0

I have two tables as shown below

ob_period

person_id     ob_start_date

  1            2007/02/11
  2            2008/05/13  
  3            2008/07/29
  4            2006/03/21

visit

person_id     visit_date

  1            2003/06/21
  2            2005/02/23  
  3            2006/04/19
  5            2004/06/11

I would like to update the "ob_start_date" of "ob_period" table with "visit_date" of visit_table

I was trying something like below for update but it can't work as I am not sure how to update with a dynamic value from another table

update ob_period a
set a.ob_start_date = b.visit_date
where a.person_id = b.person_id

I expect my output to be like as shown below

output

person_id     visit_date

  1            2003/06/21
  2            2005/02/23  
  3            2006/04/19
  4            2006/03/21

Can you help me with this please?

2 Answers 2

1

You need a table reference to the second table:

update ob_period p
    set ob_start_date = v.visit_date
    from visit v
    where p.person_id = v.person_id
Sign up to request clarification or add additional context in comments.

1 Comment

@TheGreat . . . Note that I changed the table aliases so they are meaningful abbreviations for the tables, rather than arbitrary letters.
1

Try this:

 update ob_period a
    set a.ob_start_date = b.visit_date
    from visit b
    where a.person_id = b.person_id

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.