Thursday, October 16, 2008

at 8:03 AM Posted by senthil

1) Find Nth Highest Record from Database Table - Using Ranking Function ROW_NUMBER

SELECT

t.*

FROM

(

SELECT

e1.*,

row_number() OVER (

ORDER BY e1.content_id DESC) AS Rank

FROM

Content AS e1

) AS t

WHERE

t.Rank = 3

------------------------------------------------------------------------------------------------------------


2) Find Second Highest Record from Database Table Using Top

SELECT TOP 1 content_id

FROM (

SELECT DISTINCT TOP 2 content_id

FROM content

ORDER BY content_id desc) a

ORDER BY content_id asc­­­­­­­­­­­­­­

Note: If we want third,fourth Highest Record. Just we need to Modify the above query to top 3,top 4.

------------------------------------------------------------------------------------------------------------

3) Find Second Highest Record from Database Table Using Max

SELECT MAX(content_id) FROM content

WHERE content_id NOT IN (SELECT MAX(content_id) FROM content)

------------------------------------------------------------------------------------------------------------

0 comments: