我无法弄清楚我的查询在这里做错了什么。
SELECT ORDER#, SUM(PAIDEACH * QUANTITY) AS TOTAL
FROM ORDERITEMS
WHERE TOTAL > 39.9
GROUP BY ORDER#
ORDER BY TOTAL DESC;
这是我不断得到的错误:
"TOTAL": invalid identifier
答案 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
子句。只需用数字引用该列,但不要忘记在更改查询时更改它; - )