3

我有一个名为“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  

等等

4

1 回答 1

3

试试这个查询,数据库名称是 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)
于 2012-02-23T11:42:41.487 回答