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 > DB2 > top ten values

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-18-06, 21:23
ps_deep ps_deep is offline
Registered User
 
Join Date: Aug 2006
Posts: 3
top ten values

I am a new bee in Db2, Can someone tell me how to get 10 max values from a table in Db2, earlier i was using oracle where there was rownum but in db2 w do not have this..

Thanks
Deep
Reply With Quote
  #2 (permalink)  
Old 08-19-06, 09:00
jthakrar jthakrar is offline
Registered User
 
Join Date: Mar 2004
Posts: 46
I suppose you want the top 10 rows from a table, right ?
Then you can use "fetch first 10 rows only" clause at the end of your SQL.

However note that the "first 10 rows" depends on the access path that the query takes (unless you have an order by clause or something that forces the same order of rows).
Reply With Quote
  #3 (permalink)  
Old 08-20-06, 14:11
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by ps_deep
how to get 10 max values from a table in Db2
For the 10 max values of column k of table t:
Code:
SELECT DISTINCT k FROM t
ORDER BY k DESC
FETCH FIRST 10 ROWS ONLY
OPTIMIZE FOR 1 ROW
The "standard SQL" way to obtain this would be:
Code:
SELECT t1.k FROM t AS t1, t AS t2
WHERE t1.k <= t2.k
GROUP BY t1.k
HAVING count(*) <= 10
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/

Last edited by Peter.Vanroose; 08-20-06 at 14:16.
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