0

我正在构建一个佣金报告,如果订单中存在自定义运费,则需要包含它们。下面我有两个表加入。如果OrderDetails.Custom包含'Freight''Overnight'我需要它来显示'OrderDetails.CustomPrice'.

如果OrderDetails.Custom不包含'Freight''Overnight'我需要将其OrderDetails.CustomPrice留空,但只要WHERE满足该子句,仍会显示所有其他字段。

任何帮助是极大的赞赏!


SELECT Orders.OrderDate, Orders.OrderID,      Orders.PaymentAmount, 
       Orders.Shipping,  OrderDetails.Custom, OrderDetails.CustomPrice  
FROM Orders
  JOIN OrderDetails 
    ON OrderDetails.OrderID = Orders.OrderID
WHERE OrderDate BETWEEN '04/01/12 00:00:01' 
                    AND '04/30/12 11:59:59' 
  AND SalesRep_CustomerID = '21140';
4

2 回答 2

2

您可以使用 case 语句实现您想要的:

SELECT Orders.OrderDate, Orders.OrderID, Orders.PaymentAmount, Orders.Shipping, 
CASE WHEN (OrderDetails.Custom = 'Freight' OR OrderDetails.Custom = 'Overnight') THEN OrderDetails.CustomPrice ELSE '' END AS  CustomPrice,
 OrderDetails.CustomPrice  
FROM Orders
JOIN OrderDetails ON OrderDetails.OrderID = Orders.OrderID
WHERE OrderDate BETWEEN '04/01/12 00:00:01' AND '04/30/12 11:59:59' and SalesRep_CustomerID = '21140';
于 2012-05-04T07:07:45.117 回答
0

用例语句获取“CustomPrice”列的值

于 2012-05-04T07:06:54.833 回答