I have a college SQL final in a few days, and I have a query that drives me crazy! I know this is a stupid request, but I am just starting and I canβt understand.
So, there are basically 2 tables, Client and Orders.
Client Orders --------- --------- PK Client_Id PK Order_Id Name Client_Id FK Order_Total Date
Now they ask me "Write off the name of the customer who bought the most in 2011"
So, for what I thought, this requires, on the one hand, that I have SUM all Order_Total and Group by Client since 2011, then from this table select the client with the sum of the sums of the sums MAX (), and then show only the name of this client. The problem is that I cannot figure out how to put all this in one request.
Hope someone can help!
Thank you all for your very quick answers! I am really impressed!
Now I'm not going to be picky or anything else, but just in case, my teacher does not accept the "Limit" or "Select top" instruction, is there any way to make this request without them?
Edit: attempted source code ported from comments:
SELECT C.NAME FROM CLIENTS C, ORDERS O WHERE O.CLIENT_ID = C.CLIENT_ID AND O.DATE BETWEEN '1/1/2011 00:00:00.000' and '12/31/2011 23:59:59.999' HAVING SUM(O.ORDER_TOTAL) >= ALL (SELECT SUM (O2.ORDER_TOTAL) FROM ORDER O2 GROUP BY O2.CLIENT_ID)
source share