在接下来的7天内找到最大值

发布于 2025-02-12 18:18:05 字数 1137 浏览 0 评论 0原文

我有一个SQL表:

ID日期
101/01/201950
101/13/201924
101/19/201953
201/05/201950
201/11/11/11/201924
201/24/2019/201953

我想创建一个新列,该列计算在接下来的14天内按ID分组的最大值。如果当前行中的日期与下一个日期之间的差异大于14,请返回无或null。

新表将是:

ID日期MAX_14
101/01/01/50150
101/13/20192453
101/19/201953NONE
201/05/20195050 2 50
201/11/11/20192453 24 53
201/24/201953

I have a SQL table:

iddatevalue
101/01/201950
101/13/201924
101/19/201953
201/05/201950
201/11/201924
201/24/201953

I want to create a new column that computes that max value over the next 14 days grouped by id. If the difference between the date in the current row and the next is greater than 14, return None or Null.

The new table will be:

iddatevaluemax_14
101/01/20195050
101/13/20192453
101/19/201953None
201/05/20195050
201/11/20192453
201/24/201953None

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

此生挚爱伱 2025-02-19 18:18:05

您可以为此使用子问题:

select t.*, (
    select max(value)
    from t as x
    where x.id = t.id
    and   x.date >= t.date
    and   x.date < dateadd(day, 14, t.date)
)
from t

You can use a sub-query for this:

select t.*, (
    select max(value)
    from t as x
    where x.id = t.id
    and   x.date >= t.date
    and   x.date < dateadd(day, 14, t.date)
)
from t
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文