简体   繁体   中英

how to select latest row in a table based on id from another table

I have two tables, REPORTS and REPORT_TYPE which are as described below.

REPORTS

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

I need the latest pdf and xls reports based on the creation date for every report id in REPORT_TYPE table, along with the report type description.
Report_id is primary key in report_type and foreign key in REPORTS.
The creation date will be same for both pdf and xls report for a report name.
Please comment if any more details are needed.
Any ideas?

This is what I tried but obviously didn't work. I need some logic to pass the current iterated report_type_id from outer query to inner query i guess.

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)
 Select * from REPORTS 
  where (REPORT_ID, CREATION_DATE) 
 in (
   select REPORT_ID, MAX(CREATION_DATE)
  from REPORTS
  group by REPORT_ID)

Try this:

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

For the sample data you posted, this will give you:

| 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 |

Note that: this will give you duplicate Report_ID in case there were duplicate report_id s with the same max date like in your sample data. If you want to eliminate duplicates, you can do this:

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

This will give you:

| REPORT_ID | REPORT_NAME | CREATION_DATE | REPORT_TYPE_DESC |
--------------------------------------------------------------
|       100 | Report2.pdf |   29-Nov-2012 |      ReportType1 |
|       101 | Report3.pdf |   28-Nov-2012 |      ReportType2 |
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
                        )
Select * 
  from REPORTS 
  where (REPORT_ID, CREATION_DATE) in (
    select REPORT_ID, MAX(CREATION_DATE)
      from REPORTS
      group by REPORT_ID
  ) 

This gives you all of the latest files. Obviously can be filtered.

try this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM