SQL(server 2016)-使用案例结果作为子查询的where子句

SQL(server 2016)-使用案例结果作为子查询的where子句,sql,subquery,case,where-clause,sql-server-2016,Sql,Subquery,Case,Where Clause,Sql Server 2016,我正在使用SQL Server 2016。我有一个带有案例的select语句。此案例返回国家代码。在同一select语句中,我想查询另一个表,以检索与此国家/地区代码相关的描述。为此,我创建了一个子查询。在这个子查询中,我想在where子句中使用大小写的结果 SELECT DelAddrCode , CASE WHEN DelAddrCode = '0' THEN (SELECT Customeraddress.countrycode FROM Customeraddre

我正在使用SQL Server 2016。我有一个带有案例的select语句。此案例返回国家代码。在同一select语句中,我想查询另一个表,以检索与此国家/地区代码相关的描述。为此,我创建了一个子查询。在这个子查询中,我想在where子句中使用大小写的结果

SELECT
      DelAddrCode
    , CASE   WHEN DelAddrCode = '0' THEN (SELECT Customeraddress.countrycode FROM Customeraddress WHERE Customeraddress.CustID = Salesorder.CustID)
                                    ELSE (SELECT Deliveryaddress.countrycode FROM Deliveryaddress where Deliveryaddress.CustID = Salesorder.CustID AND Deliveryaddress.DelAddrCode = Salesorder.DelAddrCode)
             END AS delivery_country
    , (SELECT country.description from Country WHERE Country.countrycode = delivery_country)
FROM Salesorder
这里的逻辑是,您有一个默认的客户地址,例如在美国,但您可以在不同的地址(如英国)交付销售订单。首先,我检索DelAddrCode。如果为0,则我从CustomerAddress表中检索countrycode。如果不是0,那么我将从Deliveryaddress表中获取countrycode

现在我有了countrycode,我想从country表中获取国家描述。我为此使用子查询。但是这个子查询的where子句取决于案例的结果。上面的查询给出了一个错误:“无效的列名‘del_country’”

我找到的解决方案是复制案例并将其放入子查询:

SELECT
      DelAddrCode
    , CASE   WHEN DelAddrCode = '0' THEN (SELECT Customeraddress.countrycode FROM Customeraddress WHERE Customeraddress.CustID = Salesorder.CustID)
                                    ELSE (SELECT Deliveryaddress.countrycode FROM Deliveryaddress where Deliveryaddress.CustID = Salesorder.CustID AND Deliveryaddress.DelAddrCode = Salesorder.DelAddrCode)
             END AS delivery_country
    , (SELECT country.description from Country WHERE Country.countrycode = 
             (CASE   WHEN DelAddrCode = '0' THEN (SELECT Customeraddress.countrycode FROM Customeraddress WHERE Customeraddress.CustID = Salesorder.CustID)
                                            ELSE (SELECT Deliveryaddress.countrycode FROM Deliveryaddress WHERE Deliveryaddress.CustID = Salesorder.CustID and Deliveryaddress.DelAddrCode = Salesorder.DelAddrCode)
                     END))
    FROM Salesorder
这给了我想要的结果,但它让代码变得一团糟。有没有一种方法可以在子查询中引用案例的结果来获得相同的结果,而不必再次复制粘贴相同的案例


提前谢谢

请尝试以下代码。它只包含一次
案例
,我发现它更具可读性

SELECT t.delAddrCode,
       country.description
FROM
(
    SELECT Salesorder.DelAddrCode,
           CASE
               WHEN Salesorder.DelAddrCode = '0' THEN Customeraddress.countrycode
               ELSE Deliveryaddress.countrycode
           END AS delivery_country
    FROM Salesorder
    LEFT JOIN Customeraddress ON Customeraddress.CustID = Salesorder.CustID
    LEFT JOIN Deliveryaddress ON Deliveryaddress.CustID = Salesorder.CustID
            AND Deliveryaddress.DelAddrCode = Salesorder.DelAddrCode
) t
LEFT JOIN Country ON country.countrycode = t.delivery_country;

外部连接地址表并查看哪一个与
COALESCE
匹配:

SELECT
  DelAddrCode,
  COALESCE(ca.countrycode, da.countrycode) AS countrycode,
  c.description
FROM Salesorder so
LEFT JOIN Customeraddress ca ON ca.CustID = so.CustID AND so.DelAddrCode = 0
LEFT JOIN Deliveryaddress da ON da.CustID = so.CustID AND da.DelAddrCode = so.DelAddrCode
JOIN country c ON c.countrycode = COALESCE(ca.countrycode, da.countrycode);
如果
Deliveryaddress.DelAddrCode
不能为0(我假设为0),您甚至可以将其设置为0

JOIN country c ON c.countrycode IN (ca.countrycode, da.countrycode);

作为一个更简单的链接,这可能会导致更好的执行计划。

您听说过
JOIN