Skip to content
Advertisement

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!

Advertisement

Answer

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
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement