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

痴心易碎 提交于 2019-12-30 10:38:12

问题


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 I could not manage.

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

Clarification: I like to get something like this:

 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  

and so on


回答1:


try this query database name test is thier u 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 1 )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)



来源:https://stackoverflow.com/questions/9411767/mysql-min-and-max-values-and-corresponding-date-for-each-month

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!