0

我正在尝试使用别名 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
4

4 回答 4

1

因此,您可以通过以下方式定义别名:

SELECT Alias = TableAlias.Field         --prefixed to a field
       ,TableAlias.Field as OtherAlias  --at the end of a field
FROM Table TableAlias                   --on a table
--then use by anything that is caculated after that part in the order of operations.
ORDER BY Alias;

然后您可以在定义它们的区域之后使用它们。

SQL 操作顺序:

  1. FROM 子句
  2. WHERE 子句
  3. GROUP BY 子句
  4. HAVING 子句
  5. 选择子句
  6. ORDER BY 子句
于 2013-04-13T02:15:36.157 回答
1

您没有提供任何示例数据,因此未经测试:

SELECT 
  t.OrderID,
  t.PartID,
  t.Description,
  t.Qty,
  t.UnitPrice,
  t.OriginalCost,
  t.QuantityDiscount,
  SUM(t.OriginalCost - t.QuantityDiscount) AS FinalCost
FROM (
    SELECT  
      Orders.Orderid, 
      Inventory.partid, 
      Description, 
      Qty, 
      (Inventory.price) AS UnitPrice, 
      (OrderItems.Qty * Inventory.price) AS OriginalCost, 
      CASE 
        WHEN OrderItems.Qty >= 5 THEN (OrderItems.Qty * Inventory.Price) * .05)
        WHEN OrderItems.Qty >= 10 THEN (OrderItems.Qty * Inventory.Price) * .10)
        ELSE 0
      END AS QuantityDiscount
    FROM 
      Orders
    JOIN 
      OrderItems ON OrderItems.orderid = OrderItems.orderid
    JOIN 
      Inventory ON ORDERITEMS.partid = Inventory.partid
) AS t
ORDER BY QTY DESC
于 2013-04-13T02:18:13.917 回答
0

您不能使用在同一个 select 语句中定义的别名。您将不得不使用子查询或表表达式。

像这样的东西(代码未经测试,只是为了给你一个想法)

SELECT * 
        ,SUM(t2.OriginalCost - t2.QuantityDiscount) AS FinalCost
FROM
  ( SELECT * 
           ,CASE WHEN t1.Qty >= 5 THEN (t1.OriginalCost * .05) WHEN t1.Qty >= 10 THEN (t1.OriginalCost * .10) ELSE 0 END AS QuantityDiscount
    FROM
          ( SELECT Orders.Orderid,
                   Inventory.partid,
                   Description,
                   Qty,
                   (Inventory.price) AS UnitPrice,
                   (OrderItems.Qty * Inventory.price) AS OriginalCost
           FROM Orders
           JOIN OrderItems ON OrderItems.orderid = OrderItems.orderid
           JOIN Inventory ON ORDERITEMS.partid = Inventory.partid 
           ) AS t1 

  ) AS t2
ORDER BY QTY DESC
于 2013-04-13T02:11:56.613 回答
0

试试这个:

SELECT
  Orderid,
  partid,
  Description,
  Qty,
  UnitPrice,
  OriginalCost,
  QuantityDiscount,
  OriginalCost  - QuantityDiscount AS FinalCost
FROM (
  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
  FROM Orders
) T
JOIN OrderItems ON OrderItems.orderid = OrderItems.orderid
JOIN Inventory ON ORDERITEMS.partid = Inventory.partid
ORDER BY QTY DESC

如果此时您仍需要 amy 聚合,请将另一个查询包装为子查询。

于 2013-04-13T02:17:55.607 回答