【问题标题】:how to select latest row in a table based on id from another table如何根据另一个表中的id选择表中的最新行
【发布时间】:2012-11-29 20:01:47
【问题描述】:

我有两个表,REPORTS 和 REPORT_TYPE,如下所述。

报告

REPORT_TYPE_ID   REPORT_NAME   CREATION_DATE
 100        Report1.pdf    28-Nov-2012
 100       Report1.xls     28-Nov-2012
 100       Report2.pdf     29-Nov-2012
 100       Report2.xls     29-Nov-2012
 101       Report3.pdf     28-Nov-2012
 101       Report3.xls     28-Nov-2012

REPORT_TYPE

 REPORT_TYPE_ID  REPORT_TYPE_DESC
  100        ReportType1
  101        ReportType2

我需要根据 REPORT_TYPE 表中每个报告 ID 的创建日期以及报告类型描述的最新 pdf 和 xls 报告。
Report_id 是report_type 中的主键和REPORTS 中的外键。
对于报告名称,pdf 和 xls 报告的创建日期将相同。
如果需要更多详细信息,请发表评论。
有什么想法吗?

这是我尝试过的,但显然没有奏效。我想我需要一些逻辑来将当前迭代的 report_type_id 从外部查询传递到内部查询。

SELECT AR.REPORT_TYPE_ID, LK.REPORT_TYPE_DESC, 
         AR.REPORT_NAME, AR.CREATION_DATE                                             
         FROM REPORTS  AR, REPORT_TYPE LK                    
         WHERE AR.REPORT_TYPE_ID = LK.REPORT_TYPE_ID
          AND AR.CREATION_DATE IN 
          (SELECT MAX (CREATION_DATE) FROM REPORTS AR, REPORT_TYPE LK  
          WHERE AR.REPORT_TYPE_ID = LK.REPORT_TYPE_ID)

【问题讨论】:

  • 到目前为止您尝试过哪些查询?

标签: sql oracle


【解决方案1】:
 Select * from REPORTS 
  where (REPORT_ID, CREATION_DATE) 
 in (
   select REPORT_ID, MAX(CREATION_DATE)
  from REPORTS
  group by REPORT_ID)

【讨论】:

    【解决方案2】:
    select  r1.report_id, r1.report_name, r1.creation_date, r2.report_type_desc
    from reports r1 join report_type r2 on r1.report_id = r2.report_id 
    where r1.creation_date in 
                            (
                             select max(creation_date) 
                             from reports 
                             where report_id = r1.report_id
                            )
    

    【讨论】:

    • 谢谢!!看起来这很有效。将使用更多数据进行一些测试。
    【解决方案3】:

    试试这个:

    SELECT 
      r.report_id,
      r.report_name, 
      r.creation_date, 
      t.REPORT_TYPE_DESC
    FROM REPORT_TYPE t
    INNER JOIN Reports r ON t.REPORT_ID = r.REPORT_ID
    INNER JOIN
    ( 
       SELECT REPORT_ID, MAX(creation_date) maxdate
      FROM reports
      GROUP BY REPORT_ID
     ) m  ON r.Creation_date = m.maxdate
         AND r.REPORT_ID = m.REPORT_ID;
    

    SQL Fiddle Demo

    对于您发布的示例数据,这将为您提供:

    | REPORT_ID | REPORT_NAME | CREATION_DATE | REPORT_TYPE_DESC |
    --------------------------------------------------------------
    |       100 | Report2.xls |   29-Nov-2012 |      ReportType1 |
    |       100 | Report2.pdf |   29-Nov-2012 |      ReportType1 |
    |       101 | Report3.xls |   28-Nov-2012 |      ReportType2 |
    |       101 | Report3.pdf |   28-Nov-2012 |      ReportType2 |
    

    请注意:这将为您提供重复的Report_ID,以防重复的report_ids 与您的示例数据中的最大日期相同。如果你想消除重复,你可以这样做:

    WITH cte
    AS
    (
      SELECT 
        r.report_id,
        r.report_name, 
        r.creation_date, 
        t.REPORT_TYPE_DESC,
        ROW_NUMBER() OVER(PARTITION BY r.report_id 
                          ORDER BY creation_date DESC) AS "rank"
      FROM REPORT_TYPE t
      INNER JOIN Reports r ON t.REPORT_ID = r.REPORT_ID
     ) 
    SELECT REPORT_ID, REPORT_NAME, CREATION_DATE,   REPORT_TYPE_DESC
    FROM CTE 
    WHERE "rank" = 1;
    

    Updated SQL Fiddle Demo

    这会给你:

    | REPORT_ID | REPORT_NAME | CREATION_DATE | REPORT_TYPE_DESC |
    --------------------------------------------------------------
    |       100 | Report2.pdf |   29-Nov-2012 |      ReportType1 |
    |       101 | Report3.pdf |   28-Nov-2012 |      ReportType2 |
    

    【讨论】:

    • 谢谢。这似乎也有效。我现在有点困惑。 Grisha 的解决方案看起来更小,连接数量也更少,但会不会有什么问题?
    • 第一个解决方案是我想要的,因为我想要 pdf 和 excel 报告文件
    • @jijo 几乎是一样的,而不是谓词IN 我用了一个额外的JOIN,但这没关系,但你必须注意来自@987654332 的NULL 值@ 在 IN 谓词中。另见我的更新,还有一种使用排名功能的方法ROW_NUMBER()
    【解决方案4】:
    Select * 
      from REPORTS 
      where (REPORT_ID, CREATION_DATE) in (
        select REPORT_ID, MAX(CREATION_DATE)
          from REPORTS
          group by REPORT_ID
      ) 
    

    这将为您提供所有最新文件。显然可以过滤掉。

    【讨论】:

      【解决方案5】:

      试试这个

      select report_name,report_type_desc inner join
       on reports.id=report_type.id 
       where creationdate=max(creationdate)
       group by reports.id,report_name
      

      【讨论】:

        猜你喜欢
        • 2012-03-23
        • 1970-01-01
        • 2017-02-11
        • 2020-07-28
        • 2014-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-16
        相关资源
        最近更新 更多