Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.misc -> Re: How do I use ROWNUM in a subquery (emulating SQL Server TOP n)
Gosta,
Seeing as Sybrand was his usual helpful self, I thought I might assist. Try the following. It uses analytics in an in-line view to window sort the EBR table into employees and their most recent record ( to get the ID etc ). It saves the self-join to the EBR table. Outside the in-line view, we just pick off a single row per employeeID.
Obviously I haven't created a bunch of tables and invented data as I just don't have time, so the following is untested. Let me know if there's any "tweaks" needed and I'll see if I can assist.
SELECT DISTINCT
e.surname
, e.firstname
FROM Employee e , ( SELECT employeeID , FIRST_VALUE(id) OVER ( PARTITION BY employeeID ORDER BY startDate DESC ) AS id , FIRST_VALUE(benefitGroupId) OVER ( PARTITION BY employeeID ORDER BY startDate DESC ) AS benefitGroupId , ROW_NUMBER() OVER ( PARTITION BY employeeID ORDER BY startDate DESC ) AS rn FROM EmployeeBenefitRecord WHERE status IN ('P', 'L', 'I') ) ebr
, EmployeePensionOption epo
, PensionOption po
, PensionScheme ps
, BenefitProgramme bp
, BenefitGroup bg
Regards
Adrian
Received on Wed Dec 01 2004 - 02:07:09 CST