If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Oracle > 2nd value??

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-20-06, 04:31
irontree23 irontree23 is offline
Registered User
 
Join Date: Jan 2006
Posts: 11
Question 2nd value??

hai,
how to select 2nd largest value and the 2nd smallest values from a column records in a table?

also how do we find out how much space is used by a table in a database?
Reply With Quote
  #2 (permalink)  
Old 01-20-06, 05:37
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 3,629
Second largest value could be found using such a query:
Code:
SELECT ename, sal
FROM (SELECT ename, sal, rank() over (ORDER BY sal DESC) salary_rank
      FROM EMP)
WHERE salary_rank = 2;
To find the second smallest value, just remove red-coloured DESC (or rename it to ASC).
Reply With Quote
  #3 (permalink)  
Old 01-20-06, 11:56
joebednarz joebednarz is offline
Registered User
 
Join Date: Dec 2003
Location: Oklahoma, USA
Posts: 354
You can do a traditional Top-N analysis query:

Code:
SELECT ename, sal
FROM (select ename, sal order by sal desc)
WHERE rownum = 2;
__________________
JoeB
save disk space, use smaller fonts
Reply With Quote
  #4 (permalink)  
Old 01-20-06, 12:43
RBARAER RBARAER is offline
Registered User
 
Join Date: Aug 2004
Location: France
Posts: 754
Quote:
Originally Posted by joebednarz
You can do a traditional Top-N analysis query:

Code:
SELECT ename, sal
FROM (select ename, sal order by sal desc)
WHERE rownum = 2;
You're right, but in terms of performance I would clearly bet on Littlefoot's version using the Rank() analytic function. Analytic functions are generally excellent for such queries. Just my 2 cents .

Regards,

rbaraer
__________________
ORA-000TK : No bind variable detected... Shared Pool Alert code 5 - Nuclear query ready .
Reply With Quote
  #5 (permalink)  
Old 01-20-06, 13:09
JMartinez JMartinez is offline
Registered User
 
Join Date: May 2004
Location: Dominican Republic
Posts: 719
Performance-wise, analytics could be dangerous some times. This is a perfect example. Litlefoot's query, will have to first select all of the rows from the EMP table in order to apply the WHERE on the analytic itself. I would say, go for it, if the table is relatively *small*. If its not, make sure you have an index on the column you're wanting to WHERE on, and do something like select * from table where id = ( select max( id ) from table where id <> ( select max( id ) from table ) ).

BTW, joebednarz's query is *wrong*
Reply With Quote
  #6 (permalink)  
Old 01-20-06, 13:45
beilstwh beilstwh is offline
Lead Application Develope
 
Join Date: Jun 2004
Location: Liverpool, NY USA
Posts: 2,222
Quote:
Originally Posted by joebednarz
You can do a traditional Top-N analysis query:

Code:
SELECT ename, sal
FROM (select ename, sal order by sal desc)
WHERE rownum = 2;
Unfortunally your query would not work. Rownum refers to the number of the row returned. "rownum = 2" will never return anything.

SQL> select object_name
2 from all_objects
3 where rownum = 1;

OBJECT_NAME
------------------------------
DUAL

SQL> edit
Wrote file afiedt.buf

1 select object_name
2 from all_objects
3* where rownum = 2
SQL> /

no rows selected


Your query could be rewritten as follows

SQL> edit
Wrote file afiedt.buf

1 select object_name
2 from
3 (select object_name,rownum rnum
4 from (select object_name
5 from all_objects
6 order by object_name desc))
7* where rnum = 2
SQL> /

OBJECT_NAME
------------------------------
sun/tools/util/CommandLine
__________________
Bill
You do not need a parachute to skydive. You only need a parachute to skydive twice.
Reply With Quote
  #7 (permalink)  
Old 01-20-06, 15:52
irontree23 irontree23 is offline
Registered User
 
Join Date: Jan 2006
Posts: 11
thanks

Quote:
Originally Posted by Littlefoot
Second largest value could be found using such a query:
Code:
SELECT ename, sal
FROM (SELECT ename, sal, rank() over (ORDER BY sal DESC) salary_rank
      FROM EMP)
WHERE salary_rank = 2;
To find the second smallest value, just remove red-coloured DESC (or rename it to ASC).


that helped a lot
thanks
Reply With Quote
  #8 (permalink)  
Old 01-22-06, 11:48
RBARAER RBARAER is offline
Registered User
 
Join Date: Aug 2004
Location: France
Posts: 754
Quote:
Originally Posted by JMartinez
BTW, joebednarz's query is *wrong*
You're right, sorry for that one .

Thank you for pointing it out and thanks to beilstwh for correcting it.

Usually I test before saying something, even if I'm "sure" about it. Here I didn't have much time, didn't test... and made a mistake !

As for analytics performance, this is not a case in which they will perform best, as you said. They do when they are used upon a small part of a table, not the whole table. Yet it's always good to think about them, test and see, don't you think ?

Regards,

rbaraer
__________________
ORA-000TK : No bind variable detected... Shared Pool Alert code 5 - Nuclear query ready .
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On