如果表2也有数据,如何仅从表1中提取数据

时间:2016-01-07 15:56:18

标签: sql sql-server-2008

我的问题是,我有两张表用于发货,这就是正在发生的事情。如果ca.addr_1有数据正常,但如果c.addr_2中有数据则显示它。我想要的是如果ca.addr在某些行上有数据使用它,但如果所有行都为null则使用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, 

1 个答案:

答案 0 :(得分:0)

情况应该允许你做你需要的事情。

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

您可以使用ELSE WHEN添加尽可能多的变体,因为您需要考虑要选择的地址结果的排列。