Friday, December 26, 2008

SQL Server 2005 Ranking records

What if we want to show a serial number along side with the list of our records, SQL server 2005 gives a simple and nice way to do that, see following query:

SELECT Row_Number() Over (Order By <column_name>) as SNO, <table_name>.*
FROM <table_name>

This query will list all records of a table with a serial number column at start, the Row_Number() function does the trick for us, it requires one column for sorting.

Result:
SNO...
1...
2...
3...
4...
....
....
....
....
....

No comments:

Post a Comment