Reputation: 815
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)
Upvotes: 0
Views: 676
Reputation: 79969
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;
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;
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 |
Upvotes: 1
Reputation: 59
Select * from REPORTS
where (REPORT_ID, CREATION_DATE)
in (
select REPORT_ID, MAX(CREATION_DATE)
from REPORTS
group by REPORT_ID)
Upvotes: 3
Reputation: 7986
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
)
Upvotes: 1
Reputation: 1888
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
Upvotes: 0
Reputation: 1247
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.
Upvotes: 0