在客户表中:
ID| Name | Cost
-------------------------
1| name1 | 1000
2| name2 | 200
3| name3 | 700
4| name2 | 700
5| name3 | 800
我需要找出前2名客户的姓名& ID&购买的客户表的费用。 任何人都可以帮助我吗?
我的预期输出:
ID| Name | Cost
----------------------
1| name1 | 1000
2| name2 | 900
答案 0 :(得分:3)
SELECT
GROUP_CONCAT(ID) as payment_ids,
Name,
SUM(Cost) as total_cost
FROM
Customer
GROUP BY
Name
ORDER BY
total_cost DESC
LIMIT 2