我正在尝试使用别名 OriginalCost 和 QuantityDiscount 进行一些计算。我了解您只能在 GROUP BY、HAVING 或 ORDER BY 中使用别名,但我不确定如何正确使用。我收到此代码的错误是
第 9 行无效的列名“QuantityDiscount”。
SELECT Orders.Orderid, Inventory.partid, Description, Qty, (Inventory.price) AS UnitPrice,
(OrderItems.Qty * Inventory.price) AS OriginalCost,
CASE
WHEN OrderItems.Qty >= 5 THEN (OriginalCost) * .05)
WHEN OrderItems.Qty >= 10 THEN (OriginalCost) * .10)
ELSE 0
END AS QuantityDiscount,
SUM(OriginalCost - QuantityDiscount) AS FinalCost
FROM Orders
JOIN OrderItems ON OrderItems.orderid = OrderItems.orderid
JOIN Inventory ON ORDERITEMS.partid = Inventory.partid
ORDER BY QTY DESC