简体   繁体   中英

How to only pull data from table 1 if table 2 also have data

My problem is, I have two tables that we are using for shipping and this is what is happening. If ca.addr_1 has data is fine but if there is data in c.addr_2 then show it. And what I want is if ca.addr has the data on some line use it but if all lines are null then use c.addr.

CO.SHIP_TO_ADDR_NO AS Addr_No,
ISNULL(CA.NAME, C.NAME) AS Name,
ISNULL(CA.ADDR_1, C.ADDR_1) AS Addr_1,
ISNULL(CA.ADDR_2, C.ADDR_2) AS Addr_2,
ISNULL(CA.ADDR_3, C.ADDR_3) AS Addr_3,
ISNULL(CA.CITY, C.CITY) AS City, 
ISNULL(CA.STATE, C.STATE) AS State,
ISNULL(CA.ZIPCODE, C.ZIPCODE) AS ZipCode,
ISNULL(CA.COUNTRY, C.COUNTRY) AS Country, 

CASE WHEN should allow you to do what you need.

CASE WHEN ca.addr1 IS NULL AND CA.ADDR_2 IS NULL AND CA.ADDR_3 IS NULL
THEN c.addr
ELSE WHEN ....

You can add in as many variations using ELSE WHEN as you need to account for whatever permutation of address results you want to select.

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