如何用另一个 table 的信息替换空数据?
How to replace null data with information from another table?
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 ship_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
使用ISNULL
函数,
ISNULL(ca.State, c.State) As ship_state
您可以使用COALESCE函数:
在你的SELECT
中:
COALESCE(ca.STATE, c.STATE) AS ship_state
如果 ca.STATE
是 NULL
,这将 return c.STATE
的值。
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 ship_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
使用ISNULL
函数,
ISNULL(ca.State, c.State) As ship_state
您可以使用COALESCE函数:
在你的SELECT
中:
COALESCE(ca.STATE, c.STATE) AS ship_state
如果 ca.STATE
是 NULL
,这将 return c.STATE
的值。