1

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

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

这是我不断收到的错误:

"TOTAL": invalid identifier
4

3 回答 3

5

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

SELECT ORDER#, SUM(PAIDEACH * QUANTITY) AS TOTAL 
  FROM ORDERITEMS 
 GROUP BY ORDER# 
HAVING SUM(PAIDEACH * QUANTITY) > 39.9
 ORDER BY TOTAL DESC; 
于 2012-06-29T02:39:06.147 回答
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;
于 2012-06-29T01:13:39.707 回答
0

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

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

于 2012-06-29T05:50:26.090 回答