Your USING subquery has no join criteria for the two instances of the table, so it is going to generate a product of all the rows in both tables. You don't want that. Perhaps what you meant to code was something like this?
MERGE
INTO housedata a
USING
(select b.parcelid
, max(b.propertyaddress) as propertyaddress
from housedata b
where b.propertyaddress is not null
group by b.parcelid
) src
ON (a.parcelid = src.parcelid)
WHEN MATCHED THEN
UPDATE
SET a.propertyaddress = src.propertyaddress
WHERE a.propertyaddress is null
Note this logic handles multiple instances of parcelid
with difference values of
propertyaddress
. Without seeing your data or knowing your business rules it's hard to know whether than is a valid use case, and whether max()
is the appropriate solution. Only you can decide that.
I have given my screenshot of my orginal table which contains null values in property address.
To give a proper answer we need to see a representative sample of the whole data set. So it would be better if you showed some data filtered or sorted by parcelid
, including rows where propertyaddress
is not null. As it is, I have to guess what your problem might be.
I do not understand how my use case is solved by the max() function.
If you only have one row per parcelid
with a populated propertyaddress
then you don't need MAX(). However, if you have more than one such instance then you need some logic to choose one. Apart from anything else, MERGE requires it to generate a stable set. It doesn't have to be MAX(): MIN() would also work, maybe some other aggregate or analytic functions too. Again, you need to choose the the best fit for your business rules.