使用另一列作为键添加值列

Add Value column using another column as Key

希望 table 本身能说明问题。本质上,对于左侧的 Type 列,是否可以根据类型的出现顺序使用 Type 作为散列 key/set 添加唯一的 code/value 列:

Type | Code
-----------
ADA  |    1
ADA  |    1
BIM  |    2
BIM  |    2
CUR  |    3
BIM  |    2
DEQ  |    4
ADA  |    1
...  |  ...

我们不能简单地对转换进行硬编码,因为每次都有任意数量的 Types。

您可以使用 dense_rank():

select type, dense_rank() over (order by type) as code
from t;

但是,我建议您创建另一个 table 并使用它:

create table Types as (
    select row_number() over (order by type) as TypeId,
           type
    from t
    group by type;

然后,加入:

select t.type, tt.TypeId
from t join
     types tt
     on t.type = tt.type;