Solved

pl/sql Order by within subquery

Posted on 2011-03-14
5
884 Views
Last Modified: 2012-05-11
Hi,

I have a subquery where I want to get the topmost record of that subquery. My query is similar to to following:

SELECT u.USER_ID, (SELECT ua.USER_ALIAS FROM USER_ALIAS ua WHERE ua.USER_ID=u.USER_ID WHERE ROWNUMBER=1 ORDER BY ua.USER_ALIAS_ID DESC)
FROM USERS u;

Open in new window


Now, I have been reading in various places that the order by within a subquery won't work but I cannot figure out how to achieve this otherwise. Are analytic functions such as RANK, PARTITION etc. any use in this situation? My understanding of them is limited .....

Any help much appreciated.

Regards,

Tomás

0
Comment
Question by:mausy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 74

Expert Comment

by:sdstuber
ID: 35127606
you must put the rownum filter in an scope than the order by.


Order by is applied to the result set, so , if you have the rownum and order by within the same scope you are restricting to an effectively random 1 row, then ordering that one row
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 35127633
unfortunately you can't push an outer table reference into a doubly nested query.


that is...  this is what you want, but the "u.user_id" loses its meaning and is unknown (invalid identifier) when placed within 2 layers of subqueries

 (SELECT user_alias
          FROM (SELECT   ua.user_alias
                    FROM user_alias ua
                   WHERE ua.user_id = u.user_id
                ORDER BY ua.user_alias_id DESC)
         WHERE ROWNUM = 1)
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 35127641
So, using analytics you could do something like this...


SELECT user_id, user_alias
  FROM (SELECT u.user_id,
               ua.user_alias,
               ROW_NUMBER() OVER (PARTITION BY u.user_id ORDER BY ua.user_alias_id DESC) rn
          FROM users u, user_alias ua
         WHERE ua.user_id = u.user_id)
 WHERE rn = 1;
0
 
LVL 74

Accepted Solution

by:
sdstuber earned 500 total points
ID: 35127659
You could use a subquery with an aggregate keep clause


the aggregate in this case isn't particularly important,  you could use MAX too
it's the KEEP clause that is doing the ordering and first row selection

SELECT u.user_id,
       (SELECT MIN(ua.user_alias) KEEP (DENSE_RANK FIRST ORDER BY user_alias_id DESC)
          FROM user_alias ua
         WHERE ua.user_id = u.user_id)
           user_alias
  FROM users u;
0
 
LVL 1

Author Closing Comment

by:mausy
ID: 35127707
Perfect. Thanks sdsuber!
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
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.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

738 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