Windowing Partition Functions (Rank(), Row_Number() and Dense_Rank())

Friday, October 2, 2009 |


Microsoft SQL Server 2005 comes up with fabulous Partitioning functions which work well in SQL Server 2008 too. Just because of ignorance or unawareness of these functions, programmer used to iterate BIG BIG loops in front-end. Without much a boring lecture, let me start over functions now.

Row_Number():  Basically Row_Number() function is used to give number to each row in result set. It plays an important and a crucial role in many difficult situations. One can use it for paging purpose too (I probably will post one SP in future article which you can directly use in your front-end for paging purpose).

--create temp table for demo
IF OBJECT_ID('tempdb..#Employee') is not null DROP TABLE #EMPLOYEE
Create Table #Employee
(
      FirstName varchar(20),
      LastName Varchar(20),
      DepartMent varchar(20)
)


--insert few records
Insert Into #Employee
SELECT 'Ritesh','Shah', 'MIS' UNION ALL
SELECT 'Rajan','Shah','ACCT' UNION ALL
SELECT 'Rajan','Mehta','ACCT' UNION ALL
SELECT 'Alka','Shah','MIS'


--simple Row_Number with Order By First Name, Last Name
--this will not make any partition and simply give row number to every row
SELECT ROW_NUMBER() over(order by FirstName,LastName) as Num,FirstName,LastName,DepartMent
FROM #Employee


--this will make a partition on First Name
--so, every first instance of FirstName will have row number 1
--you can find duplicate records with this way too. :)
SELECT ROW_NUMBER() over(Partition by FirstName order by FirstName,LastName) as Num,FirstName,LastName,DepartMent
FROM #Employee


Rank() and Dense_Rank(): These two functions mainly used to use for giving Rank to each row.  You may use it for finding Toppers based on examination results records set or maybe use it for finding few top vendors based on track records of sales you have etc. There is only one small but technically big difference between Rank() and Dense_Rank() functions which I am going to show you in practical script below which will be easy to evaluate as I am going to show you all possible partitioning function in one T-SQL.

--create temp table for demo
IF OBJECT_ID('tempdb..#SampleOrder') is not null DROP TABLE #SampleOrder
Create Table #SampleOrder
(
      OrderID Int Identity(1,1),
      ClientID int,
      TotalSample int,
      SampleDate datetime
)

--insert few records
Insert Into #SampleOrder
SELECT 1,2,GETDATE()-5 UNION ALL
SELECT 2,5,GETDATE()-8 UNION ALL
SELECT 1,22,GETDATE()-3 UNION ALL
SELECT 3,2,GETDATE()-1 UNION ALL
SELECT 1,2,GETDATE()-5


SELECT *,
            ROW_NUMBER() over(order by TotalSample) as RowNum,
            ROW_NUMBER() over(Partition By ClientID order by TotalSample) as RowNumP,
            Rank() over(order by TotalSample) as Ran,
            Rank() over(Partition By ClientID order by TotalSample) as RanP,
            Dense_Rank() over(order by TotalSample) as DRan,
            Dense_Rank() over(Partition By ClientID order by TotalSample) as DRanP
FROM #SampleOrder


If you will observe output of above T-SQL, you will get to know the difference between Rank and Dense_Rank. There is only difference, if you will get same instance based on partition, rank will give same number all, suppose we get three same instance (same clientID three time with same value) rank will give it, suppose 1 for all three and when next instance come, rank will give it 4 rather than 2. In Dense_Rank, you will get 2, it won’t break the chain.


Reference: Ritesh Shah
http://www.sqlhub.com
Note: Microsoft Books online is a default reference of all articles but examples and explanations prepared by Ritesh Shah, founder of
 http://www.SQLHub.com

0 comments: