Why does Rank() have gaps and Dense_Rank() not have gaps in ranking ties in rows?

Then encircled rows in the Rank / Dense_Rank examples below have ties for TotCnt.  Ex1 (using Rank()) has ties for TotCnt=19 and TotCnt=20.  Then TopCustomers rank jumps from 4 to 6.  In Ex2 (using Dense_Rank() for same set) has the same ties but the TopCustomers rank goes smoothly from 4 to 5 and so on incrementing by 1.  Why is this?  What is different from Rank() vs Dense_Rank()? 

Use Northwind

SELECT RANK() OVER (ORDER BY TotCnt DESC) AS TopCustomers, CustomerID, TotCnt
FROM (SELECT CustomerID, COUNT(*) AS TotCnt
FROM Orders Group BY CustomerID) AS Cust

-------------------------------------------

SELECT DENSE_RANK() OVER (ORDER BY TotCnt DESC) AS TopCustomers, CustomerID, TotCnt
FROM (SELECT CustomerID, COUNT(*) AS TotCnt
FROM Orders Group BY CustomerID) AS Cust

March 20th, 2015 6:29pm

DENSE_RANK() "Returns the rank of rows within the partition of a result set, without any gaps in the ranking. The rank of a row is one plus the number of distinct ranks that come before the row in question."

Basically, DENSE_RANK() counts the number of distinct ranks prior to the current one, and adds one to it to determine the new rank. 

https://msdn.microsoft.com/en-us/library/ms173825(v=sql.110).aspx

RANK() "Returns the rank of each row within the partition of a result set. The rank of a row is one plus the number of ranks that come before the row in question."

Basically, RANK() counts the number of ALL ranks prior to the current one, and adds one to it to determine the new rank.

https://msdn.microsoft.com/en-us/library/ms176102(v=sql.110).aspx

That difference is the reason for the two functions.

Free Windows Admin Tool Kit Click here and download it now
March 20th, 2015 6:39pm

I think I'm getting this so with Rank() is goes like

Rank  Count
1........20
2........23
3........37
3........37
5........42

with Dense_Rank

Rank  Count
1........20
2........23
3........37
3........37
4........42

March 20th, 2015 7:19pm

Yes...

It's easy if you think of it in real life terms. We can use your original screen shots for that.

SAVEA had a TotCnt of 31, putting it in 1st place (no tie).
ERNSH had a TotCnt of 30, putting it in 2nd place (no tie).
QUICK had a TotCnt of 28, putting it in 3rd place (no tie).
FOLKO & HUNGO both had a TotCnt of 19, putting them in a tie for 4th place (tie).
HILLA & BERGS both had a TotCnt of 18, putting them in a tie for ??? place (tie).

Why the "???" you ask? Well it's a mater of perspective...

If you think of them as groups (goups being members of a tie), then HILLA & BERGS came in 5th place (DENSE_RANK).
If you think of them as individuals, there were 5 individuals that were better, putting them in 6th place (RANK).

Thinking about it in those terms was how I initially got my head wrapped around the difference...

HTH,

Jason
Free Windows Admin Tool Kit Click here and download it now
March 20th, 2015 8:22pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics