[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Order by a column and then select the top row

Posted on 2008-11-19
4
Medium Priority
?
901 Views
Last Modified: 2013-12-19
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
Comment
Question by:asmyatt
  • 3
4 Comments
 
LVL 26

Expert Comment

by:tigin44
ID: 22999749
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
 
LVL 74

Expert Comment

by:sdstuber
ID: 23000107
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
 
LVL 74

Expert Comment

by:sdstuber
ID: 23000125
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
 
LVL 74

Accepted Solution

by:
sdstuber earned 2000 total points
ID: 23000136
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

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
Suggested Courses
Course of the Month18 days, 22 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question