2015-01-20 51 views
0
Order_ID  ship_state  bill_state 
    121   null   CA 
    122   WA    IN  
    123   CA    OR 
    124   null   FL 

我有一個查詢(下面),帶回這些結果(上面)。 Order_ID,Ship_state和bill_state分別位於不同的表中。我如何修改我的查詢,以便它將填充與相應的bill_state空null_state?有些訂單直接發送到帳單地址,這使得運輸字段爲空,因爲它們留空。我想要生成一份船舶狀態清單。如何用另一個表中的信息替換空數據?

SELECT ca.STATE AS ship_state, 
     co.ID, 
     co.CUSTOMER_ID, 
     ca.ADDR_NO, 
     co.SHIP_TO_ADDR_NO, 
     c.STATE AS Bill_state, 
     c.NAME AS Bill_name, 
     ca.NAME AS Ship_name, 
     col.PART_ID, 
     col.ORDER_QTY, 
     ca.COUNTRY AS Ship_Country, 
     c.COUNTRY AS Bill_country, 
     co.ORDER_DATE 
FROM dbo.CUST_ORDER_LINE AS col 
FULL OUTER JOIN dbo.CUST_ADDRESS AS ca 
    FULL OUTER JOIN dbo.CUSTOMER AS c 
     INNER JOIN dbo.CUSTOMER_ORDER AS co 
      ON c.ID = co.CUSTOMER_ID 
     ON ca.CUSTOMER_ID = co.CUSTOMER_ID 
     AND ca.ADDR_NO = co.SHIP_TO_ADDR_NO 
     AND ca.CUSTOMER_ID = c.ID 
    ON col.CUST_ORDER_ID = co.ID 
WHERE co.ORDER_DATE > '2014-1-1' 
    AND co.ID NOT LIKE 'rma%' 
ORDER BY co.ID 

回答

0

可以使用COALESCE功能:

在你SELECT

COALESCE(ca.STATE, c.STATE) AS ship_state 

這將返回的c.STATE如果ca.STATE是價值NULL

+0

非常感謝。完美的作品! – Ian 2015-01-20 19:40:51

+0

@請看看[this](http://stackoverflow.com/help/someone-answers)。 – dario 2015-01-20 19:43:51

0

使用ISNULL功能,

ISNULL(ca.State, c.State) As ship_state 
相關問題