• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 904
  • Last Modified:

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

0
asmyatt
Asked:
asmyatt
  • 3
1 Solution
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now