LLL
LLL

Reputation: 469

SQL Assign Unique number to each unique value in a column

I have a table in Snowflake with people's names and other attributes. To simplify, it looks like the table below.

enter image description here

How can I add a new column with assigned unique number to each person directly to the table using SQL? The ideal result is like below

enter image description here

Upvotes: 0

Views: 1075

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269483

Use dense_rank():

select name, dense_rank() over (order by name) as uniquenum
from t;

You can use this logic in an update, but the exact syntax depends on the database.

Upvotes: 2

Related Questions