I'm struggling to do something in SQL which I'm sure must be simple, but I can't figure it out. I want the MAX() value of a group, but I also want the value of another column in the same row as the max value. Here is an example table definition:
mysql> desc Sales;
+---------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------+------+-----+---------+-------+
| StoreID | int(11) | YES | | NULL | |
| Day | int(11) | YES | | NULL | |
| Amount | int(11) | YES | | NULL | |
+---------+---------+------+-----+---------+-------+
3 rows in set (0.00 sec)
And here is some data for it:
mysql> SELECT * FROM Sales;
+---------+------+--------+
| StoreID | Day | Amount |
+---------+------+--------+
| 1 | 1 | 44 |
| 1 | 2 | 31 |
| 1 | 3 | 91 |
| 2 | 1 | 93 |
| 2 | 2 | 32 |
| 2 | 3 | 41 |
| 3 | 1 | 48 |
| 3 | 2 | 95 |
| 3 | 3 | 12 |
+---------+------+--------+
9 rows in set (0.00 sec)
What I want to know is, what Day
had the most sales (Amount
) for each StoreID
.
Now I know I can do this:
SELECT StoreID, MAX(Amount) FROM Sales GROUP BY StoreID;
+---------+-------------+
| StoreID | MAX(Amount) |
+---------+-------------+
| 1 | 91 |
| 2 | 93 |
| 3 | 95 |
+---------+-------------+
3 rows in set (0.00 sec)
That tells me the max amount of each store, but really what I'm after is the day that it occured. But I can't add Day back in to the query because it's not in the group by, and I don't think I really want to group by that value do I?
I'm not sure where to go from here.
In short, the results I want should look like this:
+---------+------+--------+
| 1 | 3 | 91 |
| 2 | 1 | 93 |
| 3 | 2 | 95 |
+---------+------+--------+