SQL 获取值,其中某些值大于 Having 无效的值

SQL get Values where some is greater than value with Having not working

我有这个代码:

Select firstname, lastname, sum(quantity) as quantitySum
FROM Customer
JOIN "Order"
ON Customer.id = "Order".customerid
JOIN orderitem
ON "Order".id = orderitem.orderid
WHERE country = 'Germany'
GROUP BY firstname, lastname
HAVING quantitySum > 500
ORDER BY lastname

但是它给我一个错误。我想不明白。乐于助人!

Having 子句在 group by 之后执行,然后在 select 之后执行,因此您必须使用 having 的聚合函数,如下所示

Select firstname, lastname, sum(quantity) as quantitySum
FROM Customer
JOIN "Order"
ON Customer.id = "Order".customerid
JOIN orderitem
ON "Order".id = orderitem.orderid
WHERE country = 'Germany'
GROUP BY firstname, lastname
HAVING sum(quantity) > 500
ORDER BY lastname