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