我目前有这个信息,可以显示每月总金额。我想从中得到的是,显示出每年最多的月份。
SELECT year(paymentDate), month(paymentDate) , SUM(amount)
FROM classicmodels.payments
GROUP BY year(paymentDate), month(paymentDate)
ORDER BY paymentDate ASC;
这是我只想显示每年金额最高的月份的表
最佳答案
您可以为此使用ROW_NUMBER
:
SELECT y, m, amount
FROM (
SELECT YEAR(paymentDate) AS y, MONTH(paymentDate) AS m,
amount,
ROW_NUMBER() OVER (PARTITION BY YEAR(paymentDate) ORDER BY amount DESC) AS rn
FROM classicmodels.payments) AS t
WHERE t.rn = 1
注意:如果是平局,查询将返回任意月份。要返回所有金额最大的月份,请使用
RANK
而不是ROW_NUMBER
。关于mysql - SQL,获得每年最高的付款月份,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46461522/