Below is my Table
Table1
+--------+----------+---------+
| amount | make | product |
+--------+----------+---------+
| 100 | Nokia | Mo
You can use this solution to retrieve the "group-wise maximum" based on the amount
:
SELECT a.*
FROM Table1 a
INNER JOIN Table1 b ON a.product = b.product AND a.amount <= b.amount
GROUP BY a.amount, a.product
HAVING COUNT(*) <= 2
Simply change the 2
to however many of the top rows you want to retrieve per product.
If you wanted to retrieve the lowest two rows per product, you can simply change the <=
sign in the INNER JOIN
to a >=
.
You can fiddle around with this solution here: SQL-Fiddle Demo