MySQL最小最大历史值

huangapple go评论60阅读模式
英文:

MYSQL Min Max Historic Values

问题

我有一个MYSQL表格,格式如下:

保险 日期 价值
A 2020-01-01 2
A 2020-02-01 5
A 2020-03-01 4
A 2020-04-01 6
A 2020-05-01 8
A 2020-06-01 1
A 2020-07-01 3
A 2020-08-01 7
A 2020-09-01 9
A 2020-10-01 3
A 2020-11-01 5
A 2020-12-01 6
B 2020-01-01 5
...

我想从这种方式中选择最小值和最大值(历史值):

在日期 2020-01-01 最小 2 最大 2
在日期 2020-02-01 最小 2 最大 5
在日期 2020-03-01 最小 2 最大 5
在日期 2020-04-01 最小 2 最大 6
在日期 2020-05-01 最小 2 最大 8
在日期 2020-06-01 最小 1 最大 8
在日期 2020-07-01 最小 1 最大 8
在日期 2020-08-01 最小 1 最大 8
在日期 2020-09-01 最小 1 最大 9
在日期 2020-10-01 最小 1 最大 9
在日期 2020-11-01 最小 1 最大 9
在日期 2020-12-01 最小 1 最大 9

我尝试找到解决方案,但只能在日期之间获取最小值和最大值,而不是逐月参考上个月。

你能帮我吗?谢谢!

英文:

I have a MYSQL table with this format:

Insurance Date Value
A 2020-01-01 2
A 2020-02-01 5
A 2020-03-01 4
A 2020-04-01 6
A 2020-05-01 8
A 2020-06-01 1
A 2020-07-01 3
A 2020-08-01 7
A 2020-09-01 9
A 2020-10-01 3
A 2020-11-01 5
A 2020-12-01 6
B 2020-01-01 5
...

I want SELECT Min and Max from this way (historic value)

in date 2020-01-01 Min 2 Max 2
in date 2020-02-01 Min 2 Max 5
in date 2020-03-01 Min 2 Max 5
in date 2020-04-01 Min 2 Max 6
In date 2020-05-01 Min 2 Max 8
In date 2020-06-01 Min 1 Max 8
In date 2020-07-01 Min 1 Max 8
In date 2020-08-01 Min 1 Max 8
In date 2020-09-01 Min 1 Max 9
In date 2020-10-01 Min 1 Max 9
In date 2020-11-01 Min 1 Max 9
In date 2020-12-01 Min 1 Max 9

T try to fin the solution, but only I get min and max value from a between dates, but not month to month with reference at last months.

Can you help me? THANKS

答案1

得分: 0

尝试以下查询以从表中获取日期的最小值和最大值:

SELECT DISTINCT date, MAX(value) AS max_value, MIN(value) AS min_value
FROM table_name GROUP BY date;
英文:

Try the follow query to get min and max value of a date from table: -

SELECT DISTINCT date, MAX(value) AS max_value, MIN(value) as min_value
FROM table_name group by data;

答案2

得分: 0

你似乎正在寻找在同一保险的所有先前行中进行窗口最小/最大值。您可以使用窗口函数(在 MySQL 8.0 中可用):

select t.*,
    min(val) over(partition by insurance order by dt) min_val_so_far,
    max(val) over(partition by insurance order by dt) max_val_so_far
from mytable t

在 DB Fiddle 上的演示

保险 dt val min_val_so_far max_val_so_far
A 2020-01-01 2 2 2
A 2020-02-01 5 2 5
A 2020-03-01 4 2 5
A 2020-04-01 6 2 6
A 2020-05-01 8 2 8
A 2020-06-01 1 1 8
A 2020-07-01 3 1 8
A 2020-08-01 7 1 8
A 2020-09-01 9 1 9
A 2020-10-01 3 1 9
A 2020-11-01 5 1 9
A 2020-12-01 6 1 9
英文:

You seem to be looking for a window min/max over all preceding rows of the same insurance. You can use window functions (available in MySQL 8.0):

select t.*,
    min(val) over(partition by insurance order by dt) min_val_so_far,
    max(val) over(partition by insurance order by dt) max_val_so_far
from mytable t

Demo on DB Fiddle:

insurance dt val min_val_so_far max_val_so_far
A 2020-01-01 2 2 2
A 2020-02-01 5 2 5
A 2020-03-01 4 2 5
A 2020-04-01 6 2 6
A 2020-05-01 8 2 8
A 2020-06-01 1 1 8
A 2020-07-01 3 1 8
A 2020-08-01 7 1 8
A 2020-09-01 9 1 9
A 2020-10-01 3 1 9
A 2020-11-01 5 1 9
A 2020-12-01 6 1 9

huangapple
  • 本文由 发表于 2023年6月15日 03:12:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76476853.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定