Geeks Talk

Prepare for your Next Interview




DB2 query

This is a discussion on DB2 query within the DB2 forums, part of the Databases category; How to retrieve the third maximum value from a column ? For Ex: If i have emp-id as one column with values 111,458,785,896,274,698 etc, using ...


Go Back   Geeks Talk > Databases > DB2

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 09-18-2007
Junior Member
 
Join Date: Feb 2007
Location: Chennai
Posts: 6
Thanks: 0
Thanked 1 Time in 1 Post
kanchhana is on a distinguished road
DB2 query

How to retrieve the third maximum value from a column ?
For Ex: If i have emp-id as one column with values 111,458,785,896,274,698 etc, using SELECT MAX(EMP-ID) retrieves the value "896". But i need to retrieve third maximum value ie., 698 incase of above example. How to write query for this ?
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 09-20-2007
Contributing Member
 
Join Date: Sep 2007
Posts: 36
Thanks: 1
Thanked 3 Times in 3 Posts
ssuvi is on a distinguished road
Re: DB2 query

Query to select top nth record:

consider the table name as emp,then

select empid
from emp e1
where ( n =
( select count ( distinct ( e2.empid ) )
from emp e2
where e2.empid >=e1.empid))

replace n by 3 to get the third maximum
Reply With Quote
  #3 (permalink)  
Old 12-28-2007
Junior Member
 
Join Date: Feb 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
teddy is on a distinguished road
Re: DB2 query

select max(empid) from table1
where empid not in (select distinct(empid) from table1
order by empid desc
fetch first 2 rows only)
Reply With Quote
Reply

  Geeks Talk > Databases > DB2


Thread Tools
Display Modes


Similar Threads

Thread Thread Starter Forum Replies Last Post
Query sakshi_2801 SQL 4 08-06-2007 10:29 AM
regarding sql query psuresh1982 SQL 8 07-13-2007 03:20 AM
Query jescalante Oracle 4 06-25-2007 02:13 AM
Query K.K.Rajiv SQL 2 05-29-2007 10:41 PM
Query with C++ StephenRaj C and C++ 1 08-14-2006 04:09 AM


All times are GMT -4. The time now is 02:35 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Copyright © 2008 GeekInterview.com. All Rights Reserved