[英]Fifty two week high and low
我们有一个包含以下数据的记录表。 我们需要在 ft_high 和 ft_low 列中填写 52 周(或 365 天)的高低值吗? 我们如何在 MySQL 中实现这一点?
五十二周的数据,包括同一日期。
id user_id date amount ft_high ft_low
10 21 2020-10-11 1500 1800 950
11 22 2020-10-12 1950 2410 1738
12 21 2020-10-15 1150 1800 1500
----------------------------------------
----------------------------------------
99 21 2020-11-15 1950 1950 950
您可以使用 window 函数:
selet t.*,
min(amount) over (partition by user_id order by date range between interval 52 week preceding and current row) as ft_low,
max(amount) over (partition by user_id order by date range between interval 52 week preceding and current row) as ft_high,
from t;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.