6

我有兴趣制作一个从两个不同列中提取值的案例语句,其中一个列涉及计算。我不确定如何进行这项工作,但我会向您展示我到目前为止所拥有的。(我有一列显示项目类型,例如特殊订单项目、停产项目和正常库存的项目。第二列显示手头数量 - 销售订单数量以确定该项目是否有库存。)这里是我的查询

SELECT ItemID, ItemType, (QuantityOnHand - QuantityonSalesOrders) AS Instock
CASE
WHEN ItemType = 'SP / OR' THEN 'Special Order' 
WHEN Instock < '1' THEN 'Out of Stock' 
WHEN Instock > '0' THEN 'In Stock' 
AS "Stock" FROM peachtree;
4

1 回答 1

9

对于这种类型的请求,您将不得不重用计算或使用子查询。这是因为您提供Instock值的别名在选择列表中不可使用:

SELECT ItemID, 
    ItemType, 
    Instock,
    CASE
        WHEN ItemType = 'SP / OR' THEN 'Special Order' 
        WHEN Instock < '1' THEN 'Out of Stock' 
        WHEN Instock > '0' THEN 'In Stock' 
    END AS "Stock" 
FROM
(
    select ItemID, ItemType, (QuantityOnHand - QuantityonSalesOrders) AS Instock
    from peachtree
) p;

或者:

SELECT ItemID, 
    ItemType, 
    (QuantityOnHand - QuantityonSalesOrders) as Instock,
    CASE
        WHEN ItemType = 'SP / OR' THEN 'Special Order' 
        WHEN (QuantityOnHand - QuantityonSalesOrders) < '1' THEN 'Out of Stock' 
        WHEN (QuantityOnHand - QuantityonSalesOrders) > '0' THEN 'In Stock' 
    END AS "Stock" 
FROM peachtree
于 2013-01-09T16:47:33.643 回答