Skip to content
Advertisement

Rank Values by Partition in SQL

I am trying to create a new column that counts unique partitions of another column.

 ColA *NewCol
 ------------
 A    1
 A    1
 A    1
 B    2
 B    2
 C    3
 C    3
 C    3

I have tried using

DENSE_RANK() OVER (PARTITION BY ColA ORDER BY ColA)

but each partition’s count stays the same at 1.

How would I go about getting results for *NewCol?

Advertisement

Answer

You are looking for just the order by

select dense_rank(*) over (order by cola)

The partition by causes the value to be 1 for all cola, because only one value is considered in each partition.

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