我有一个包含以下列的表
Store_ID, Item_ID, Sales_Amount
我需要检索每个商店的两个畅销商品。有人对这个问题有想法或建议吗?
答案 0 :(得分:2)
这应该有效
SELECT mytable.Store_ID, mytable.Sales_Amount
FROM mytable
LEFT JOIN mytable table2 ON mytable.Store_ID = table2.Store_ID
AND mytable.Sales_Amount <= table2.Sales_Amount
GROUP BY mytable.Store_ID, mytable.Sales_Amount HAVING COUNT(mytable.*) <= 2;