Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

My Experiments with SQLServer
Browse by Tags · View All
SQLServer 126
SQLServer 2008 R2 91
SQLServer 2008 86
SQLServer 2005 82
SQL 60
Database 59
Sql And Me 57
Tips & Tricks 28
SQL Server 27
SQL FAQ 26

Archive · View All
May 2011 25
June 2011 21
July 2011 21
August 2011 19
April 2011 16
January 2013 4
May 2012 3
April 2012 3
October 2011 3
February 2013 2

Vishal Gajjar's Blog

SQL Functions – RANK() & DENSE_RANK()

Jun 29 2011 8:46PM by Vishal Gajjar   

The RANK() functions ranks each row of a result set. It can also be used to partition the data for ranking. It takes two arguments, PARTITION BY clause and ORDER BY clause. As the name suggests PARTITION BY clause is used to partition the result set into chunks, and ORDER BY defines the order of the rows.

For example,

-- © 2011 – Vishal (http://SqlAndMe.com)
 
USE   AdventureWorks2008R2
 
SELECT      TOP (10)
            OrganizationLevel, JobTitle, LoginID, HireDate,
            RANK() OVER (ORDER BY HireDate ASC) AS 'Rank'
FROM        HumanResources.Employee

Result Set:

In the above example, RANK() function is used to rank each row based on HireDate, no partitioning is used. Now if we need to rank all rows for each OrganizationLevel individually, we need to add PARTITION BY clause to RANK() as below:

SELECT      TOP (10)
            OrganizationLevel, JobTitle, LoginID, HireDate,
            RANK() OVER (PARTITION BY OrganizationLevel
                        ORDER BY HireDate ASC) AS 'Rank'
FROM  HumanResources.Employee

Result Set:

In this case, for OrganizationLevel = 1, the ranking again starts from 1, same way for OrganizationLevel = 2.

However, RANK() functions does not always assign consecutive numbers to rows, if you look at the first example, there is a tie while assigning rank 8, in this case both rows are assigned rank 8, and the next rank assigned is 10.

In cases where you need consecutive numbers to be assigned, you can use DENSE_RANK():

SELECT      TOP (10)
            OrganizationLevel, JobTitle, LoginID, HireDate,
            DENSE_RANK() OVER (ORDER BY HireDate ASC) AS 'Rank'
FROM  HumanResources.Employee

Result Set:

Hope This Helps! Cheers!


Republished from Sql&Me [31 clicks].  Read the original version here [32134 clicks].

Vishal Gajjar
46 · 4% · 1276
0
Liked
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

Your Comment


Sign Up or Login to post a comment.

    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]