SQL – Finding Nth Highest Row In DB Table

Generally I come across situations where I have to find nth highest row in a SQL table. The following piece of SQL code is very handy to me to deal with such situations. I thought of posting it to my blog thinking that it might help you either. Please replace N with proper level of highestness required, e.g. 1 (1st highest), 2 (2nd highest) and MYTABLE with your table, and MYCOLUMN with the column that you are trying to find the nth highest value from.

SELECT * FROM MYTABLE T1
WHERE (N =
(SELECT COUNT(DISTINCT (T2.MYCOLUMN))
FROM MYTABLE T2
WHERE T2.MYCOLUMN >= T1.MYCOLUMN))

I have tested this code and it runs perfect.

Originally posted 2008-07-23 20:32:36.

Share

How to setup and run the Microsoft’s Command Line BCP Utility Without Installing SQL Server Client?

Did your run into situations where you wanted to insert records into your MS SQL Server via bulk copy (bcp) but did not have the SQL Server client installed on your machine? Well I Did. My server was in a different network machine and the machine I was working on locally did not have the SQL Server client installed.

So here is how I managed to download run the BCP utility without needing a SQL Server client installation.

Downloaded the following two files from microsoft’s Website.

http://www.microsoft.com/en-us/download/details.aspx?id=16177

  • SQL Native Client: sqlncli.msi
  • SQL Command Line Utilities: SqlCmdLnUtils.msi

Install these files and then you will be able to run the bcp. In my machine, the bcp command line utility was located at:

C:\Program Files\Microsoft SQL Server\100\Tools\Binn

There is no specific license required for this. I believe, the command line utilities are provided by microsoft for free.

Blog Widget by LinkWithin
Share