0%
Loading ...

Ranking Functions

Percentrank

This article refers to AdventureWorks2014 of Microsoft.

google ads placeholder

You can download AdventureWorks2014 DB here.


At the following picture there are 4 ranking functions at MS SQL:

1. Row_number – Numbering each row.

2. Rank –ย According to example below when postal code change then the function displays the value of row_number at that position.

3. Dense_Rank – According to example below when postal code change then the function increases.

4. NTile – Divide the results into groups, the example shows division to 4 groups.

Ranking Ranking Functions
Ranking Functions

You can copy and paste the following text into you MS SQL management studio if order to test it:

SELECTย ย ย ย ย p.firstname,
 p.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ย ASย s
 INNERย JOINย person.personย ย ย ย ย ASย p
 ONย ย ย ย ย ย ย ย ย s.businessentityidย =ย p.businessentityid
 INNERย JOINย person.addressย ASย a
 ONย ย ย ย ย ย ย ย ย a.addressidย =ย p.businessentityid
 WHEREย ย ย ย ย ย territoryidย ISย NOTย NULL
 ANDย ย ย ย ย ย ย ย salesytdย <>ย 0;
Ranking Functions
Ranking Functions