SQL在其中的子句中未识别列别名
SQL 在此脚本的 WHERE 子句中存在问题:
SELECT
ITEM_ID, ITEM_PRICE, DISCOUNT_AMOUNT, QUANTITY,
(ITEM_PRICE*QUANTITY) AS price_total,
(DISCOUNT_AMOUNT*QUANTITY) AS discount_total,
((ITEM_PRICE-DISCOUNT_AMOUNT)*QUANTITY) AS item_total
FROM ORDER_ITEMS
WHERE item_total > 500
ORDER BY item_total;
我收到此错误:
Error starting at line : 1 in command -
SELECT
ITEM_ID, ITEM_PRICE, DISCOUNT_AMOUNT, QUANTITY,
(ITEM_PRICE*QUANTITY) AS price_total,
(DISCOUNT_AMOUNT*QUANTITY) AS discount_total,
((ITEM_PRICE-DISCOUNT_AMOUNT)*QUANTITY) AS item_total
FROM ORDER_ITEMS
WHERE item_total > 500
ORDER BY item_total DESC;
Error at Command Line : 7 Column : 7
Error report -
SQL Error: ORA-00904: "ITEM_TOTAL": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
为什么price_total 和discount_total 都没有问题,但报告item_total 无效?
我试图首先仅选择减去折扣金额并乘以数量后总数大于 500 的商品。然后,我需要按 item_total 对结果进行降序排序。
SQL is having an issue with the WHERE clause of this script:
SELECT
ITEM_ID, ITEM_PRICE, DISCOUNT_AMOUNT, QUANTITY,
(ITEM_PRICE*QUANTITY) AS price_total,
(DISCOUNT_AMOUNT*QUANTITY) AS discount_total,
((ITEM_PRICE-DISCOUNT_AMOUNT)*QUANTITY) AS item_total
FROM ORDER_ITEMS
WHERE item_total > 500
ORDER BY item_total;
I am receiving this error:
Error starting at line : 1 in command -
SELECT
ITEM_ID, ITEM_PRICE, DISCOUNT_AMOUNT, QUANTITY,
(ITEM_PRICE*QUANTITY) AS price_total,
(DISCOUNT_AMOUNT*QUANTITY) AS discount_total,
((ITEM_PRICE-DISCOUNT_AMOUNT)*QUANTITY) AS item_total
FROM ORDER_ITEMS
WHERE item_total > 500
ORDER BY item_total DESC;
Error at Command Line : 7 Column : 7
Error report -
SQL Error: ORA-00904: "ITEM_TOTAL": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Why has it no issue with price_total nor discount_total, but is reporting item_total as invalid?
I am trying to first select only the items which have a total greater than 500 when the discount amount is subtracted and it is multiplied by the quantity. Then, I need to sort the results in descending order by item_total.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因此,以下查询是非法的:
列允许
您可以在以下情况下参考列中的列别名:
,例如
So, the following query is illegal:
The column alias is allowed in:
You could refer to the column alias in WHERE clause in the following cases:
For example,
从 Oracle 12c 开始,您可以使用 CROSS APPLY 定义表达式,然后可以在 WHERE 子句中引用它们:
Starting from Oracle 12c you could use
CROSS APPLY
to define expression and then you could refer to them inWHERE
clause:您不能在查询原因中使用用作别名的列名
:
查询将在当时首先检查运行时的列名称“ item_total”在表“ order_items”中找不到,因为它是作为别名的给出的不存储在任何地方,而是在所需的输出中分配该列仅
替代:
如果您要使用该类型使用sub询问,则性能不好,但它是替代方式之一
You cannot use the column name which is used as alias one in the query
Reason:
The query will first checks for runtime at that time the column name "item_total" is not found in the table "ORDER_ITEMS" because it was give as alias which is not stored in anywhere and you are assigning that column in desired output only
Alternate:
If you want to use that type go with sub queries it's performance is not good but it is one of the alternate way