(Simplified example). I have this table:
USER ID | code | sales
1 x 100
1 y 200
1 z 150
2 x 300
2 z 17
2 y 100
For each user, I need to show just the record with the max sales.
USER ID | code | sales
1 y 200
2 x 300
In the real table I have, It is millions of records. What would be the most efficient way of doing this?
Right now I find for each user the max sales record and join it unto itself to find the full record.
SELECT * from T
WHERE sales = (SELECT MAX(sales) FROM T WHERE user_id=outr.user_id)
Let's assume that sales number does not repeat itself for a specific user.