【问题标题】:SQL Query for Latest Snapshot From Bitemporal Data从双时态数据获取最新快照的 SQL 查询
【发布时间】:2022-06-19 02:43:21
【问题描述】:

给定一个包含 bitemporal modeling 的数据表,其中有 2 个日期:(i) 数据适用的日期,以及 (ii) 已知事实的日期。

City    Temperature  Date         As_of_Datetime
----    -----------  ----         --------------
Boston  32           2022/07/01   2022/06/28 13:23:00
Boston  31           2022/07/01   2022/06/29 12:00:00
Miami   81           2022/07/01   2022/06/28 13:23:00
Miami   85           2022/07/01   2022/06/29 12:00:00

什么 SQL 查询会根据As_of_Datetime 给出数据日期的最新快照? 例如

City    Temperature  Date         
----    -----------  ----         
Boston  31           2022/07/01   
Miami   85           2022/07/01   

【问题讨论】:

    标签: sql bi-temporal point-in-time


    【解决方案1】:
    select
      t1.*
    from
      temperature_table t1,
      (select max(As_of_Datetime) as max_as_of, “City”, “Date” from temperature_table group by “City”, “Date”) t_temp
    where
      t1.”City” = t_temp.”City” and t1.”Date” = t_temp.”Date” and t1.”As_of_Datetime” = t_temp.”max_as_of”
    order by
      t1.”City”, t1.”Date”
    

    【讨论】:

      猜你喜欢
      • 2022-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多