Skip to main content

Row_Number(), Rank() and Dense_Rank()

Three Functions of SQL Server:
1) Row_Number() -- To get the Unique order of Rows
2) Rank() – To rank the Records with skipped Rows
3) Dense_Rank() – To rank the Records without skipping any Rows

Lets prepare base for these:
1) Create a table like this:
CREATE TABLE [dbo].[Student](
[StudentID] [int] IDENTITY(1,1) NOT NULL,
[StudentName] [varchar](50) NULL,
[Age] [int] NULL
) ON [PRIMARY]

2) Insert data into that like this:

3) Now run the below Query:
SELECT *
,ROW_NUMBER() OVER (ORDER BY age DESC) [Unique Order]
,RANK() OVER (ORDER BY age DESC) [Skipped Order]
,DENSE_RANK() OVER (ORDER BY age DESC) [Multiple Order]
FROM student

If u closely look at the above result set [Unique Order] column represent the Unique row number of each row which we got using Row_Number().
[Skipped Order] column represent the order of Rank which skips the number. This may be use in situation where u need to find out the rank of student based on marks, or rank of participants based on points..
[Multiple Order] column represents the intelligent Rank which does not skip the number. This may be used in situation where u need to select employees based on their salaries.
Difference between Rank() and Dense_Rank() is while assigning the next rank (after the common values rows) to the row RANK will consider the total count of rows before that row and DESNE_RANK will just give next rank according to the value.

I hope u understand what I tried to say..

Comments

Popular posts from this blog

Custom Effective Paging With Sorting across all pages GridView in Asp.Net

Hi, I need to comeback to blogging anyhow, since it was too long break. Hence yesterday  i decided to start blogging again, and thought of this topic.  When Microsoft shipped GridView control with Asp.net, it came with lot of new functionality, but it also lacking of existing functionality  like 'VirtualCount' property  which was available with DataGrid but not with GridView. Since because of this it is very much difficult to implement Custom Paging with Grid View.  Now what is custom paging and default paging?  To answer this question i will take an example. I need to display records on a page in tabular format, with paging and sorting.  Well if you go with default paging and default sorting your development work is easy. But with it there is a drawback. - It is grid view control which decides what records to display and sorted by which column. So for that it need complete set of all records. Suppose if you want to display only 25 records, a...

Ways to Execute SSIS Package in SQL2K5

Problem: One of the Junior SQL Server Developers in my company approached me yesterday with a dilemma. He was developing an SSIS Package which imports data from a comma separated text file and he wanted to know the different ways in which one can execute an SSIS Package in SQL Server 2005 and higher versions. At first I started to tell him, but figured it would be smarter to document the options and share the information. Solution: In SQL Server 2005 and higher versions there are different ways in which one can execute an SSIS package. Let us go through each option one by one. Execute SSIS Package Using SQL Server Business Intelligence Development Studio (BIDS) During the development phase of the project developers can test the SSIS package execution by running the package from Business Intelligence Development Studio a.k.a. BIDS. 1. In Solution Explorer, right click the SSIS project folder that contains the package which you want to run and then click properties as shown in the snipp...

Puzzle

Print from 1 to 100 sequentially with out using cursor, while or any other iterative loop. Ans: We can achieve this by using Common Table Expression (CTE). CTE, are a new construct introduced in Microsoft SQL Server 2005 that offer a more readable form of the derived table that can be declared once and referenced multiple times in a query. Moreover, CTEs can be recursively defined, allowing a recursive entity to be enumerated without the need for recursive stored procedures. We can achieve the above using recursive CTE: Please execute the below statements: WITH Series (Number) AS ( SELECT 1 UNION ALL SELECT Number +1 FROM Series WHERE Number 100 ) Select * from Series And see the result