I want to print maximum sum amount per month with the user who has done the maximum amount. My table structure is
+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| customername | varchar(20) | YES | | NULL | |
| processed_at | date | YES | | NULL | |
| amount | int(11) | YES | | NULL | |
+--------------+-------------+------+-----+---------+-------+
and the query to print maximum sum month wise is
SELECT MAX(A.AMT), A.month
FROM ( SELECT customername,SUM(amount) AS AMT, EXTRACT( month from processed_at) as month
FROM payments
GROUP BY customername,month) AS A
GROUP BY 2;
this is giving the result
| MAX(A.AMT) | month |
+------------+-------+
| 1900 | 4 |
| 2400 | 3 |
is there a way in which customername can also be mapped to maximum amount?