MYSQL selecciona la suma máxima de compras

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.

Intenté lo siguiente

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.

¿Puede usted ayudar?

preguntado el 10 de marzo de 12 a las 13:03

I think you have an invalid sql syntax because you are joining GRP from an unknown table. And I believe it id Orders o. -

3 Respuestas

If I understand your question correctly, you want to return the UserID which has the maximum SUM (en total) of purchases. So the above records will result:

UserID  Total Amount
  2         12

And the simpliest solution would be:

SELECT UserID, SUM(AMOUNT) as TotalAmount
FROM Orders

I'm ready to edit this if I'm wrong. :-)
(PS: Please add your desired result)


Derived from you query:

  Select  MAX(SUM(o.Amount)) as sum_ammount, 
          o.userId as UID 
    FROM  Orders o
GROUP BY  o.userID
ORDER BY  sum_ammount DESC
   LIMIT  1

respondido 11 mar '12, 00:03

you downvoted me... right?? Don't worry I will upvote you... :) - Fahim Parkar

Unfortunately, second query does not work it brings exactly the same result as Select SUM(o.Amount) as sum_ammount, o.userId as UID FROM Orders o GROUP BY (o.userID) - danny.lesnik

did the first query answers your question? I just updated my second query. - John Woo

This will not handle more than one user having the same max sum. - will hamilton

sadly the second query does not work for me, it throws "Error Code: 1111. Invalid use of group function" - Pedro Joaquín

See below, its working.

SELECT userId, sum(Amount) as 'Total'
FROM Orders 
GROUP BY userId 

La salida es

userId + Total
2      + 12

I also tried after adding new row as

insert into Orders values (9,1,0,'2010-12-10 12:12:12',10)

La salida es

userId + Total
1      + 21

respondido 10 mar '12, 14:03

what's the difference between this and my answer? - John Woo

@johntotetwoo : I didn't saw your query even... I just tested and answered here... - Fahim Parkar

@johntotetwoo : And adding new data and testing is enough to tell you that I tested at my end... Gotcha??? - Fahim Parkar

I already reversed my vote anyway it does not matter at all. Good day! - John Woo

I prefer to have this solution

SELECT UserID, SUM(AMOUNT) as TotalAmount
FROM Orders
having SUM(AMOUNT) = (select sum(amount) as col1
                 from orders
                 group by userid
                 order by  col1 desc
                 limit 1

This sql will show all users who have max purchases

Just try to change amount of orderid 1 to be 2 , then this sql will show both.

respondido 11 mar '12, 22:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.