简体   繁体   中英

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

I have this code:

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

But it gives me an error. I can not figure it out. Happy for every help!

Having clause execute after group by and then select so you have to use aggregated function with having as following

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM