Skip to content
Advertisement

How can I assign unique value to each duplicate value in a column in a table

I have data in my table as below

Table having duplicate data

Now I want to generate unique rank value to each duplicate value like 1,2,3,4 etc as below

Table with unique rank value

How to do this in SQL Server 2016?

Advertisement

Answer

You seem to be looking for row_number():

select t.*,
       row_number() over (partition by id, name order by deptid) as ranking
from t;
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement