我有一个使用GROUP BY X的SELECT。它在投影中使用SUM。有没有办法对SUM进行排序?具有最高值的组结果应该在结果表中排在第一位。我怀疑没有办法对这个SUM进行排序,因为当每个组完成它的输出并且消失了;没有任何“集合”可以排序。我必须在这里做一些完全不同的事情。你有任何提示吗?
谢谢。
(初级)
答案 0 :(得分:1)
如果我理解你的问题,答案很简单:
SELECT customer, SUM(amount) FROM mytable GROUP BY customer ORDER BY SUM(amount);
答案 1 :(得分:0)
I'm not sure you meant this but here is an example
assume that there is data in a table which shows payments people have made to a bank.
But people pay different money in different times.
*id* *payment* *time*
1 10 01 04 2016
2 20 01 02 2016
1 35 14 03 2016
3 22 21 01 2016
2 50 01 04 2016
Now you want to calculate who has paid max money.
SELECT person_id, MAX(payment) AS totalPayment
from payments
group by person_id
ORDER BY totalPayment DESC