每个月的MySQL最小值和最大值以及相应的“日期”

问题描述 投票:3回答:1

我有一个名为“ rates”的表,它有两个字段“ date”和“ rate”。我喜欢获取MIN和MAX费率值及其每个月发生的日期。但我无法管理。

选择日期,MIN(rate)AS minRate,MAX(rate)AS maxRate,MONTH(date)AS monthName,YEAR(date)AS yearName从率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  

依此类推

mysql date max minimum
1个回答
2
投票

尝试此查询,数据库名称为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)
© www.soinside.com 2019 - 2024. All rights reserved.