【问题标题】:How to get the max ID for every Type every Date如何在每个日期获取每个类型的最大 ID
【发布时间】:2021-04-18 15:25:05
【问题描述】:

我想为每个日期(Date)的每个类型(Types)保留最高的报告 ID(Report_ID

注意:数据列有多个日期,下图仅显示01.01.2021。

  SELECT a.*
  FROM Table1 a
  JOIN (
    SELECT Date, MAX(Report_ID) as maxID
    FROM Table1
    GROUP BY Date) b 
  ON a.Report_ID = b.maxID
  WHERE a.Date = '2021-01-01'
  ORDER BY Date desc

【问题讨论】:

    标签: sql greatest-n-per-group


    【解决方案1】:

    您可以使用相关子查询:

    select t.*
    from t
    where t.report_id = (select max(t2.report_id)
                         from t t2
                         where t2.date = t.date and t2.type = t.type
                        );
    

    【讨论】:

      猜你喜欢
      • 2021-05-08
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      相关资源
      最近更新 更多