I have the following Table named Order
Orders Table
___________________________________________________
orderiD | userId | OrderType | Order_Date | Amount
________|________|___________|____________|________
1 1 0 12/12/2009 1
2 1 1 13/12/2009 2
3 1 1 14/12/2009 3
4 2 0 12/12/2009 4
5 2 1 16/12/2009 2
6 1 0 14/12/2009 5
7 2 1 17/12/2009 4
8 2 0 10/12/2010 2
___________________________________________________
I need to create query which returns user id with maximum SUM of purchases.
I tried the following
Select MAX(GRP.sumAmmount), o.userId join
(Select SUM(o.Amount) as sum_ammount, o.userId as UID from Orders GROUP BY(o.userID)) as GRP on o.userId=GRP.UID GROUP BY(GRP.UID)
But I believe I’m missing something.
Can you help?
If I understand your question correctly, you want to return the
UserIDwhich has the maximum SUM (total) of purchases. So the above records will result:And the simpliest solution would be:
I’m ready to edit this if I’m wrong. 🙂
(PS: Please add your desired result)
Thanks.
UPDATE 1
Derived from you query: