我有这个mysql表:
date | client | total
2015-01-01 | john | 85.00
2015-01-01 | alfred | 35.00
2015-01-01 | georgy | 125.00
2015-01-01 | linda | 42.00
2015-01-01 | alex | 76.00
2015-01-01 | john | 94.00
2015-01-01 | john | 75.30
我想按客户名称分组计算总金额和百分比,所以我提出了这个问题:
SELECT client, total, round(total*100/t.th,1) as percent
FROM (
SELECT client, sum(total) as total
FROM mytable
WHERE date='2015-01-01'
GROUP BY `client`) c
JOIN ( select sum(total) as th from mytable
WHERE date='2015-01-01') t
order by percent desc
获得这个结果非常有用:
client | total | percent
john | 254.3 | 47.8
georgy | 125 | 23.5
alex | 76 | 14.3
linda | 42 | 7.9
alfred | 35 | 6.6
但我不知道是否可以将最后一行和一组合并为一个获得这样的结果,例如:
client | total | percent
john | 254.3 | 47.8
georgy | 125 | 23.5
others | 135 | 28.8
我想知道如何修改我的查询以获得此结果。
我想要一些帮助。