简体   繁体   中英

Replace NULL values with another column and updating the table

'Can't update the table with the new column.' 'Replacing NULL column from another column'

Select a.ParcelID, a.PropertyAddress, b.ParcelID, b.PropertyAddress, (SELECT CASE WHEN a.PropertyAddress IS NULL THEN b.PropertyAddress ELSE a.PropertyAddress END AS column_alias)
    From nashvillehousing a
    JOIN nashvillehousing b
        on a.ParcelID = b.ParcelID
        AND a.UniqueID <> b.UniqueID
    Where a.PropertyAddress is null
    
    Update nashvillehousing
    SET PropertyAddress = (SELECT CASE WHEN a.PropertyAddress IS NULL THEN b.PropertyAddress ELSE a.PropertyAddress END AS column_alias
    From nashvillehousing a
    JOIN nashvillehousing b
        on a.ParcelID = b.ParcelID
    Where a.PropertyAddress is null)

I am not sure exactly what you are trying to do, but the scalar subquery seems a bit unusual. If this is SQL Server, have you tried this? For other RDBMS, you will have to check the syntax when doing an UPDATE with a self join.

UPDATE a
SET PropertyAddress = 
    CASE 
        WHEN a.PropertyAddress IS NULL THEN b.PropertyAddress 
        ELSE a.PropertyAddress 
    END 
FROM nashvillehousing a
INNER JOIN nashvillehousing b ON a.ParcelID = b.ParcelID
WHERE a.PropertyAddress IS NULL;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM