【问题标题】:Selecting last ID value of the same day [duplicate]选择同一天的最后一个ID值[重复]
【发布时间】:2021-09-18 21:17:24
【问题描述】:

我有一个示例表,如下所示

ID Date Info
1 15.02.2020 a
2 15.02.2020 b
1 15.02.2020 c
1 15.02.2020 d
3 15.02.2020 e
1 16.02.2020 f
3 16.02.2020 g
3 16.02.2020 h

我需要创建一个选择语句来显示同一天每个 ID 的最后一行。 如下所示。

ID Date Info
2 15.02.2020 b
1 15.02.2020 d
3 15.02.2020 e
1 16.02.2020 f
3 16.02.2020 h

如何在 Oracle SQL 中管理它?

【问题讨论】:

    标签: sql oracle greatest-n-per-group


    【解决方案1】:

    一种方法使用相关子查询:

    select t.*
    from t
    where t.id = (select max(t2.id)
                  from t t2
                  where t2.info = t.info and t2.date = t.date
                 );
    

    你也可以使用窗口函数:

    select t.*
    from (select t.*,
                 row_number() over (partition by info, date order by id desc) as seqnum
          from t
         ) t
    where seqnum = 1;
    

    【讨论】:

      猜你喜欢
      • 2017-12-07
      • 2011-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-23
      • 1970-01-01
      • 1970-01-01
      • 2011-03-09
      相关资源
      最近更新 更多