Order by a column and then select the top row

I'm not an Oracle expert, so I'm having trouble figuring out this simple query.

I have two tables, they have a foreign key with a 1 to many relationship. Meaning I have a list of equipment in 1 table , and a list of events associated with the equipment in the other table .  

Each event has a timestamp column. All I want to do is sort by timestamp DESC and select the top 1 for each event. Esentially for each piece of equipment 1 select the most recent event.

Here's a stab I took at the query...but it doesn't work because the inner query selects the first row before it sorts them. What's the correct way to get around this in Oracle?

Thank for your help!

SELECT e.ID, ee.eventname, ee.eventtime FROM equipment e
LEFT JOIN
( 
SELECT eventname, eventtime FROM events where rownum = 1 
ORDER BY eventtime DESC
) ee
ON ee.equipmentid = e.id

Open in new window

LVL 2
asmyattAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tigin44Commented:
try this
SELECT e.ID, ee.eventname, MAX(ee.eventtime )
FROM equipment e
INNER JOIN events ee ON ee.equipmentid = e.id
GROUP BY e.ID, ee.eventname
ORDER BY e.ID, ee.eventname
0
sdstuberCommented:
try this...

SELECT   e.id, ee.eventname, ee.eventtime
  FROM   equipment e
         LEFT JOIN (SELECT   eventname, eventtime, equipmentid
                      FROM   (SELECT   eventname,
                                       eventtime,
                                       equipmentid,
                                       ROW_NUMBER()
                                           OVER (PARTITION BY equipment_id ORDER BY eventtime DESC)
                                           rn
                                FROM   events)
                     WHERE   rn = 1) ee
             ON ee.equipmentid = e.id
0
sdstuberCommented:
or this...


  SELECT   e.id,
           MIN(eventname) KEEP (DENSE_RANK FIRST ORDER BY eventtime DESC) eventname,
           MAX(eventtime) KEEP (DENSE_RANK LAST ORDER BY eventtime DESC) eventtime
    FROM   equipment e LEFT JOIN events ee ON ee.equipmentid = e.id
GROUP BY   e.id;
0
sdstuberCommented:
oops...  ignore previous...

SELECT   e.id,
           MIN(eventname) KEEP (DENSE_RANK FIRST ORDER BY eventtime DESC) eventname,
           MIN(eventtime) KEEP (DENSE_RANK FIRST ORDER BY eventtime DESC) eventtime
    FROM   equipment e LEFT JOIN events ee ON ee.equipmentid = e.id
GROUP BY   e.id;

or

SELECT   e.id,
           MIN(eventname) KEEP (DENSE_RANK LAST ORDER BY eventtime  eventname,
           MIN(eventtime) KEEP (DENSE_RANK LAST ORDER BY eventtime) eventtime
    FROM   equipment e LEFT JOIN events ee ON ee.equipmentid = e.id
GROUP BY   e.id;


or

SELECT   e.id,
           MIN(eventname) KEEP (DENSE_RANK FIRST ORDER BY eventtime DESC) eventname,
           MAX(eventtime)  eventtime
    FROM   equipment e LEFT JOIN events ee ON ee.equipmentid = e.id
GROUP BY   e.id;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.