我无法弄清楚我在这里的查询做错了什么。
SELECT ORDER#, SUM(PAIDEACH * QUANTITY) AS TOTAL
FROM ORDERITEMS
WHERE TOTAL > 39.9
GROUP BY ORDER#
ORDER BY TOTAL DESC;
这是我不断收到的错误:
"TOTAL": invalid identifier
由于您希望在聚合函数上有一个谓词,因此可以使用HAVING
子句
SELECT ORDER#, SUM(PAIDEACH * QUANTITY) AS TOTAL
FROM ORDERITEMS
GROUP BY ORDER#
HAVING SUM(PAIDEACH * QUANTITY) > 39.9
ORDER BY TOTAL DESC;
如果要在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;
order by 2 desc
(对列的数字引用以 1 开头)
无需编写嵌套查询,甚至无需为查询having
添加子句。只需用数字引用列,但不要忘记在更改查询时更改它;-)