如何获得最新的日期价值

发布于 2025-02-09 21:37:31 字数 1284 浏览 0 评论 0原文

我有一个带有日期和价值的表,但是如何创建一个检查器以将值1给出最新记录的值1?

原始

StartDateClientIdValueUpdateDate
01-10-202210110001-10-2022
01-10-20221012001 20011-10-2022
01-10-202210130020-10-2022
01-10-2022 01-10-20221022220011-20011-- 10-2022
01-10-202210230020-10-2022

更新

启动端客户端更新端口过时
01-10-202210110001-10-20220
01-10-2022101 2001 200120011-10-20220
01-2022 0 01-10 -202210130020-10-20221
01-10-20221022002 20011-10-20220
01-10-202210230020-10-20221

I have a table with a date and value , but how to i create a checker to give a value 1 to the most up to date record?

Original

StartDateClientIDValueUpdatedDate
01-10-202210110001-10-2022
01-10-202210120011-10-2022
01-10-202210130020-10-2022
01-10-202210220011-10-2022
01-10-202210230020-10-2022

Updated

StartDateClientIDValueUpdatedDateoutdated
01-10-202210110001-10-20220
01-10-202210120011-10-20220
01-10-202210130020-10-20221
01-10-202210220011-10-20220
01-10-202210230020-10-20221

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

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

发布评论

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

评论(2

慢慢从新开始 2025-02-16 21:37:31

使用窗口的最大值:

SELECT *, UpdatedDate = MAX(UpdatedDate) OVER(PARTITION BY ClientId) AS Outdated
FROM tab

Using windowed MAX:

SELECT *, UpdatedDate = MAX(UpdatedDate) OVER(PARTITION BY ClientId) AS Outdated
FROM tab
2025-02-16 21:37:31

这是一个精选语句,将产生您显示的输出 - 可以在数据顶部建立视图。

如果您希望将“过时的”指标作为数据库中的持续价值,请告知:

select
    *,
    1 as outdated
from
    client_values
where
    (start_date, client_id, update_date) in (
        select
            start_date,
            client_id,
            max(update_date)
        from
            client_values
        group by
            start_date,
            client_id
    )
union
select
    *,
    0 as outdated
from
    client_values
where
    (start_date, client_id, update_date) NOT in (
        select
            start_date,
            client_id,
            max(update_date)
        from
            client_values
        group by
            start_date,
            client_id
    );

This is a select statement that will produce the output you're showing - this could be established as a view on top of your data.

If you're looking to maintain the "outdated" indicator as a persistent value in the DB, please advise:

select
    *,
    1 as outdated
from
    client_values
where
    (start_date, client_id, update_date) in (
        select
            start_date,
            client_id,
            max(update_date)
        from
            client_values
        group by
            start_date,
            client_id
    )
union
select
    *,
    0 as outdated
from
    client_values
where
    (start_date, client_id, update_date) NOT in (
        select
            start_date,
            client_id,
            max(update_date)
        from
            client_values
        group by
            start_date,
            client_id
    );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文