如何根据另一个表中的id选择表中的最新行

时间:2012-12-12 06:47:11

标签: sql oracle

我有两个表,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)

5 个答案:

答案 0 :(得分:3)

 Select * from REPORTS 
  where (REPORT_ID, CREATION_DATE) 
 in (
   select REPORT_ID, MAX(CREATION_DATE)
  from REPORTS
  group by REPORT_ID)

答案 1 :(得分:1)

试试这个:

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_id。如果要消除重复项,可以执行以下操作:

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 |

答案 2 :(得分:1)

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 :(得分:0)

Select * 
  from REPORTS 
  where (REPORT_ID, CREATION_DATE) in (
    select REPORT_ID, MAX(CREATION_DATE)
      from REPORTS
      group by REPORT_ID
  ) 

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

答案 4 :(得分:0)

试试这个

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