我有兴趣制作一个案例陈述,从两个不同的列中提取值,其中一个涉及计算。我不知道如何使这个工作,但我会告诉你到目前为止我有什么。 (我有一列显示项目类型,例如特殊订单项目,已停产的物品和通常库存的物品。第二列显示现有数量 - 销售订单数量以确定物料是否有库存。)这是我的查询
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;
答案 0 :(得分: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