2

I have a table in PostgreSQL with a column "country" in which there are 32 distinct string values, but far more rows

How is it possible to assign every unique string a specific numeric value (e.g. 'Austria' = 1, 'Australia' = 2,..., 'USA' = 32) I could use the UPDATE statement, however, in this case, doing manually 32 countries is not a good way, I guess. I hope there is another way around.

Ivan.S
  • 23
  • 6

2 Answers2

1

In Postgres, here in one method:

update country c
    set col = cc.seqnum
    from (select c.*, row_number() over (order by country) as seqnum
          from country c
         ) cc
    where cc.country = c.country;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
-1

You can easily add a new ID column in the country table to cater for this.

See the syntax below:

Alter Table country
Add Id Int Identity(1, 1)
Go
MEdwin
  • 2,940
  • 1
  • 14
  • 27