I have a table of real estate data with lots of NULLS in address column. I want to replace these nulls based on the unique ID. (I’m following Alex the Analyst tutorial). However when I use this in postgresql, I’m getting an error:
ERROR: relation "a" does not exist
UPDATE a SET
propertyaddress = COALESCE(a.propertyaddress, b.propertyaddress)
FROM nashville_housing_data a
JOIN nashville_housing_data b
ON a.parcelid = b.parcelid
AND a.uniqueid <> b.uniqueid
WHERE propertyaddress is null
Now, when I replaced a
with the table name, it updated all address rows to one specific address.
2
Answers
You can’t UPDATE aliases; you must use the table name.
Also,
COALESCE
is useless here because the conditionWHERE propertyaddress is null
guarantees the first term is always null, so just assignb.propertyaddress
.Try this:
If there can be more than one other rows with a matching parcelid, break the tie with MAX():
The Issue here is with the table alias ‘a’. You cannot directly reference the updated table using an alias in the UPDATE statement. Instead, it would help if you directly referenced the table name. Try using the query with the alias ‘a’.