如何修复Oracle 11G SQL查询?

时间:2012-06-29 00:56:43

标签: sql oracle

我无法弄清楚我的查询在这里做错了什么。

SELECT ORDER#, SUM(PAIDEACH * QUANTITY) AS TOTAL 
    FROM ORDERITEMS 
    WHERE TOTAL > 39.9 
    GROUP BY ORDER# 
    ORDER BY TOTAL DESC; 

这是我不断得到的错误:

"TOTAL": invalid identifier

3 个答案:

答案 0 :(得分:5)

由于您希望在聚合函数上有谓词,因此可以使用HAVING子句

SELECT ORDER#, SUM(PAIDEACH * QUANTITY) AS TOTAL 
  FROM ORDERITEMS 
 GROUP BY ORDER# 
HAVING SUM(PAIDEACH * QUANTITY) > 39.9
 ORDER BY TOTAL DESC; 

答案 1 :(得分:2)

如果您想在WHERE子句中使用别名,则可以将查询置于另一个SELECT内:

SELECT *
FROM
(
    SELECT ORDER#, SUM(PAIDEACH * QUANTITY) AS TOTAL 
    FROM ORDERITEMS 
    GROUP BY ORDER# 
) x
WHERE x.TOTAL > 39.9 
ORDER BY x.TOTAL DESC;

答案 2 :(得分:0)

order by 2 desc(列的数字引用以1开头)

无需编写嵌套查询,甚至无需在查询中添加having子句。只需用数字引用该列,但不要忘记在更改查询时更改它; - )