Mysql min and max values and corresponding “date” for each month

前端 未结 1 1040
北荒
北荒 2021-01-14 08:10

I have a table named \"rates\" and it has two fields \"date\" and \"rate\". I like to get MIN and MAX rate values and their dates on which they occurred for each month. But

相关标签:
1条回答
  • 2021-01-14 09:03

    Try this query, the database name is test, you can use yours or remove it:

    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)
    
    0 讨论(0)
提交回复
热议问题