0

I have a table A where I need to update the value for some rows that contain nulls in a sampling column, based on the value of the corresponding column in another table B.

Table A looks like this:

id_col grouping
001 A
002 B
003 NULL
004 A
005 NULL
006 B

and table B contains:

id_col grouping
003 A
005 B

What I want to obtain is to update the grouping column of table A with the grouping in table B, for the matching id_col, which means that, after the query, table A should look like this:

id_col grouping
001 A
002 B
003 A
004 A
005 B
006 B
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
SteP
  • 49
  • 6
  • `JOIN` your tables. What have you tried? Why didn't it work? – Thom A Oct 03 '22 at 15:49
  • We need your **full** `CREATE TABLE` statements. Don't give us unhelpful cryptic names like "Table A" and "`id_col`". – Dai Oct 03 '22 at 15:51
  • https://stackoverflow.com/questions/224732/sql-update-from-one-table-to-another-based-on-a-id-match provides examples SQL SERVER `Update.. Set ..FROM.. JOIN..on..WHERE yourfield is null – xQbert Oct 03 '22 at 15:58
  • @Larnu If I join the tables I'll have two grouping columns, A.grouping and B.grouping. I need an UPDATE statement. Or if there is a JOIN statement that can change A.grouping in place, please let me know. – SteP Oct 03 '22 at 16:01
  • 1
    *"I need an UPDATE statement."* Yes, so `JOIN` in your `UPDATE` statement. If you are getting an ambiguous column error, then make sure you alias your tables and qualify your columns. @xQbert gave you a great example in their [comment](https://stackoverflow.com/questions/73937832/how-to-update-nulls-in-a-sql-table-using-the-matching-values-from-another-table?noredirect=1#comment130551560_73937832) – Thom A Oct 03 '22 at 16:02
  • I've read the post in @xQbert's comment and understood it :). Thank you, this can be closed. – SteP Oct 03 '22 at 16:11
  • Excellent; no wheel re-inventing – xQbert Oct 03 '22 at 16:12

0 Answers0