MySQL最小最大历史值

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

MYSQL Min Max Historic Values

问题

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

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

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

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

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

你能帮我吗?谢谢!

英文:

I have a MYSQL table with this format:

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

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

  1. in date 2020-01-01 Min 2 Max 2
  2. in date 2020-02-01 Min 2 Max 5
  3. in date 2020-03-01 Min 2 Max 5
  4. in date 2020-04-01 Min 2 Max 6
  5. In date 2020-05-01 Min 2 Max 8
  6. In date 2020-06-01 Min 1 Max 8
  7. In date 2020-07-01 Min 1 Max 8
  8. In date 2020-08-01 Min 1 Max 8
  9. In date 2020-09-01 Min 1 Max 9
  10. In date 2020-10-01 Min 1 Max 9
  11. In date 2020-11-01 Min 1 Max 9
  12. 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

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

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

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

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

答案2

得分: 0

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

  1. select t.*,
  2. min(val) over(partition by insurance order by dt) min_val_so_far,
  3. max(val) over(partition by insurance order by dt) max_val_so_far
  4. 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):

  1. select t.*,
  2. min(val) over(partition by insurance order by dt) min_val_so_far,
  3. max(val) over(partition by insurance order by dt) max_val_so_far
  4. 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:

确定