我有一个名为“rates”的表,它有两个字段“date”和“rate”。我喜欢获取每个月的 MIN 和 MAX 汇率值及其发生日期。但我做不到。
SELECT date,
MIN(rate) AS minRate,
MAX(rate) AS maxRate,
MONTH(date) AS monthName,
YEAR(date) AS yearName
FROM rates
GROUP BY yearName ASC, monthName ASC
澄清:我喜欢得到这样的东西:
Months MIN mindate MAX maxdate
Jan 1.234 2012-01-13 1.534 2012-01-24
Feb 1.165 2012-02-28 1.373 2012-02-11
等等
最佳答案
试试这个查询,数据库名称是test,你可以使用你的或者删除它:
SELECT
MIN(rate) AS minRate,
(select date from test.rates where rate = min(co.rate) and
month(date) = month(co.date) and year(date) = year(co.date) limit
)as min_date,
MAX(rate) AS maxRate,
(select date from test.rates where rate = max(co.rate) and
month(date) = month(co.date) and year(date) = year(co.date) limit 1) as
max_date
FROM test.rates co
GROUP BY year(date) , month(date)
关于Mysql每个月的最小值和最大值以及对应的 "date",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9411767/