【问题标题】:Get max ID for every Type and every Date from a lookup table从查找表中获取每个类型和每个日期的最大 ID
【发布时间】:2021-05-08 17:48:26
【问题描述】:

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

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

问题: t1 是我需要使用的查找表,我的挑战是它不包含供参考的日期列。

select t2.*
from t2
where t1.Report_ID = (select max(t1.Report_ID)
                     from t1
                     where t2.Date = ??? and t2.Types = ???
                    );

t1

Report_ID Name Value
1 Name 1 Value 1
2 Name 2 Value 2
3 Name 3 Value 3

t2

Date Types Report_ID Name
01.01.2020 Type 1 1 Name 1
01.01.2020 Type 1 2 Name 2
01.01.2020 Type 3 3 Name 3

查看

Date Types Name Value Report_ID
01.01.2020 Type 1 Name 2 Value 2 2
01.01.2020 Type 3 Name 3 Value 3 3

【问题讨论】:

  • 用您正在使用的数据库标记您的问题。
  • 说的很清楚是SQL。
  • do not post images of data。您可以查看SQL tag wiki 以查看如何创建可回答的 SQL 问题。
  • @astentx 已更正。谢谢。
  • @MoeAmine 您不需要 t1 中的日期列作为参考,因为您在两个表中都有 report_id。

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


【解决方案1】:

您可以通过 row_number() 和 CTE 轻松实现。首先我们需要连接 t1 和 t2 以从 t1 获取 value 列。我们使用 row_number() 在每一行中放置一个序列号,从给定日期的特定类型的最高 Report_ID 到最低。 然后我们只考虑具有最低序列号的行,它代表给定数据的任何特定类型的最高 report_id。

With cte as
  (
select t2.date,t2.types,t2.report_id,t2.name ,t1.value ,row_number () over (partition by date,types order by t2.report_id desc) RowNumber 
    from t2 inner join  t1 on t2.report_id=t1.report_id 
  )
  select  date_format(date,"%Y.%m.%d") date,types,name,value,report_id from cte where RowNumber=1
  

输出:

【讨论】:

    【解决方案2】:

    使用ROW_NUMBER()

    WITH cte AS (
      SELECT t2.*, t1.Value, 
             ROW_NUMBER() OVER(PARTITION BY `Date`, Types ORDER BY Report_ID DESC) AS rn
      FROM t2
      JOIN t1 ON t1.Report_ID = t2.Report_ID
    )
    SELECT * FROM cte WHERE rn = 1;
    

    db<>fiddle demo

    【讨论】:

      【解决方案3】:

      使用此查询:

      SELECT Date, Types, MAX(Report_ID) Report_ID
      FROM t2
      GROUP BY Date, Types
      

      您将获得每个 DateTypes 的最大 Report_ID

      加入t1:

      SELECT t2.Date, t2.Types, t1.Name, t1.Value, t1.Report_ID
      FROM t1 
      INNER JOIN (
        SELECT Date, Types, MAX(Report_ID) Report_ID
        FROM t2
        GROUP BY Date, Types
      ) t2 ON t2.Report_ID = t1.Report_ID
      

      请参阅demo
      结果:

      Date Types Name Value Report_ID
      2020-01-01 Type 1 Name 2 Value 2 2
      2020-01-01 Type 3 Name 3 Value 3 3

      【讨论】:

        【解决方案4】:

        这回答了问题的原始版本。

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

        此操作不需要参考表。您的逻辑应该在子查询中使用 t2 执行您想要的操作:

        select t2.*
        from t2
        where t2.Report_ID = (select max(tt2.Report_ID)
                              from t2 tt2
                              where tt2.Date = t2.date and tt2.Type = t2.Type
                             );
        

        【讨论】:

        • 对不起,我加错了输入t1。它实际上没有。
        【解决方案5】:

        你可以使用NOT EXISTS如下:

        select t2.*
          from t2 
          --join t1 on t1.Report_ID = t2.Report_ID -- use it if you want data from t1 in SELECT 
        where not exists 
              (select 1 from t2 t22 
                where t22.date = t2.date and t22.type = t2.type 
                  and t22.Report_ID > t2.Report_ID) 
        

        【讨论】:

        • 对不起,我加错了输入t1。它实际上没有。
        猜你喜欢
        • 2021-04-18
        • 2020-02-12
        • 1970-01-01
        • 1970-01-01
        • 2021-12-24
        • 2023-03-07
        • 1970-01-01
        • 2018-04-16
        • 1970-01-01
        相关资源
        最近更新 更多