下面是我的桌子
表格1
+--------+----------+---------+ | amount | make | product | +--------+----------+---------+ | 100 | Nokia | Mobiles | | 300 | Samesung | Mobiles | | 700 | Micromax | Mobiles | | 1000 | Karbonn | Mobiles | | 500 | Lava | Mobiles | | 100 | Floyer | Gift | | 500 | Arichies | Gift | | 300 | Feeling | Gift | +--------+----------+---------+
现在,我要显示每种产品的两个最高金额…
所以我想建立一个单一的SQL查询,它给我的结果如下..
+--------+----------+---------+ | amount | make | product | +--------+----------+---------+ | 1000 | Karbonn | Mobiles | | 700 | Micromax | Mobiles | | 500 | Arichies | Gift | | 300 | Feeling | Gift | +--------+----------+---------+
请帮助我建立这样的查询..
您可以使用此解决方案根据以下内容来检索“ 逐组最大值 ” amount:
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
只需将更2改为每个产品要检索的 头 几行。
2
如果要检索每个产品的最低两行,则只需将中的<=符号更改为INNER JOIN即可>=。
<=
INNER JOIN
>=