I have not written about this subject for long time, as I strongly believe that Book On Line explains this concept very well. SQL Server 2005 has total of 4 ranking function. Ranking functions return a ranking value for each row in a partition. All the ranking functions are non-deterministic.
ROW_NUMBER () OVER ([<partition_by_clause>] <order_by_clause>)
Returns the sequential number of a row within a partition of a result set, starting at 1 for the first row in each partition.
RANK () OVER ([<partition_by_clause>] <order_by_clause>)
Returns the rank of each row within the partition of a result set.
DENSE_RANK () OVER ([<partition_by_clause>] <order_by_clause>)
Returns the rank of rows within the partition of a result set, without any gaps in the ranking.
NTILE (integer_expression) OVER ([<partition_by_clause>] <order_by_clause>)
Distributes the rows in an ordered partition into a specified number of groups.
All the above definition and syntax are taken from BOL. It is difficult to explain above function anything else than what they are explained in BOL. Following example is excellent example from BOL again. This function explains usage of all the four function together in one query.
USE AdventureWorks;
GO
SELECT c.FirstName, c.LastName
,ROW_NUMBER() OVER (
ORDER BY a.PostalCode) AS 'Row Number'
,RANK() OVER (
ORDER BY a.PostalCode) AS 'Rank'
,DENSE_RANK() OVER (
ORDER BY a.PostalCode) AS 'Dense Rank'
,NTILE(4) OVER (
ORDER BY a.PostalCode) AS 'Quartile'
,s.SalesYTD, a.PostalCode
FROM Sales.SalesPerson s
INNER JOIN Person.Contact c
ON s.SalesPersonID = c.ContactID
INNER JOIN Person.Address a
ON a.AddressID = c.ContactID
WHERE TerritoryID IS NOT NULL
AND SalesYTD <> 0;
Resultset:

Most of the content of this article is taken from BOL.
Reference : Pinal Dave (http://www.SQLAuthority.com), ROW_NUMBER, RANK, DENSE_RANK, NTILE






Hi Pinal,
Can we use ROW_NUMBER() function in views ?
If not then what is the alternative method to give the row numbers in view
Thanks
-Sudhir
Hi Pinal,
I have MS SQL 2000 on production server and as you know these functions are not available on same. My developer has used these functions extensively in application and insisting me to upgrade MS SQL 2000 to MS SQL 2005. This is not correct time for me to upgrade my system so can you please tell me the way out. Is it possible to use these functions in MS SQL 2000 by installing some patches or sp or by installing only t-sql of MS SQL 2005.
Thanks
Vikram
yes we can use try this……………..
CREATE VIEW testview AS SELECT ROW_NUMBER() OVER (ORDER BY )-1 as autoIndex,
* FROM
CREATE VIEW testview AS SELECT ROW_NUMBER() OVER (ORDER BY )-1 as autoIndex,
* FROM
hello,
Please clafiry this doubt In MYSQL query can use like this
select * FROM `table` WHERE MATCH (`email`) AGAINST (’+firstname.lastname@hotmail.com’);
where incase of MSSQL what quey can use to select record from database exact records ..for example in textbox i’m entering chennai,software,IT. i want all the records related to this string .. please reply
Hi,
It was an amazing article with apt example.
AWESOME STUFF!!!
how to get values like this is sqlserver query?
Order Count,1,2,1-2,5,63,5-63,64,64,64,65,80,65-80,