How to top nth element in a column of a table?

There may be ways to slove this problem, and it found easy in this soluntion.

CREATE PROC spforNelement  

(

      @Nelement INT

)

AS

BEGIN TRY

;

WITH tbltemp_agan AS

(

      SELECT *,

            RANK() OVER( ORDER BY Salary DESC) AS RANKID

      FROM Employee

)

SELECT * FROM tbltemp_agan WHERE RankID=@Nelement

END TRY

BEGIN CATCH

        RAISERROR ('An error occured in the NonFatal table',10,1)

END CATCH


This procedure is to find nth element in a column of table.

 

Here we use temp table for saving the rank of each row for a required column by using RANK() function. Then save the output of the phsycal table along with a extra column as RANKID into a temp table.

      Now accept a input parameter from procedure and compare it with in the column RANKID in the where clause as shown above. This is one way of finding top nth element in a column of a table.. Happy coding.
 

Regards,

Agan.


Mr. Praveen N. Mulukutla - Software Engineer
I like to write articles on Programming languages. The idea of articles come from my own experiences while working in those areas and i like to share my knowledge on POP3 services with all others so that it might be helpful.
Service is my Motto & Knowledge is Divine
http://www.mulukutlas.blogspot.com
Read moreRead more about Contributor