简体   繁体   English

MySQL每个月的“ rate”最小值和最大值

[英]Mysql minimum and maximum values of “rate” for each month

I have a table with two fields - a field for "date" entered as day/month/year and a field "rate" 我有一个包含两个字段的表格-输入为“日期”的字段作为日/月/年,输入“费率”字段

date | 日期| rate

24/01/05 | 2007年1月24日| 1.9754 1.9754
26/01/05 | 2005年1月26日| 1.3723 1.3723
... ...
and so on 等等

So, I like to find minimum and maximum values of "rate" for each month of year(s). 因此,我希望找到一年中每个月的“比率”的最小值和最大值。 My query selects only one row 我的查询仅选择一行

SELECT DISTINCT DATE_FORMAT(date, '%d-%m-%y') as Date, MIN(rate) as r, MAX(rate) as mr FROM rates 选择DISTINCT DATE_FORMAT(日期,'%d-%m-%y')作为日期,MIN(比率)作为r,MAX(比率)作为mR FROM比率

This will get the rate values for each month and year 这将获得每个月和每年的费率值

SELECT
    YEAR(date) AS thisYear,
    MONTH(date) AS thisMonth,
    MIN(rate) AS minRate,
    MAX(rate) AS maxRate
FROM rates
GROUP BY thisYear ASC, thisMonth ASC

If you need to have the individual date(s) on which the min or max occurs you'll need some additional grouping in there, however the above should suffice for your original question. 如果您需要确定发生最大值或最小值的各个日期,则需要在其中进行一些附加分组,但是以上内容足以满足您的原始问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM