Skip to content
Advertisement

How to apply dense rank/ partition to animal table? SQL Server

Table:

Okay so in this table I want the groups ranked 1, 2, 3.

Because if animal height width footlength and brand is the same give the rows the same ranking.

The way I have coded it in SQL I keep getting the same rank for each row. vs 3 different ranks.

Any tips on how to better understand dense rank and this problem?

Advertisement

Answer

You may use DENSE_RANK as follows:

screen capture of demo below

Demo

Note that we don’t need/want a PARTITION BY clause with DENSE_RANK here, because the rank is to be applied to the entire table. Using a partition would mean that potentially the same rank values would appear more than once.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement