PostgreSQL 语句,用于更改行中的静态字段以按顺序编号



我有如下表示的数据:

App_id    Type
123       Dependent
987       Dependent
456       Dependent

我想用PostgreSQL编写一个CASE语句,以便它反映如下:

App_id    Type
123       1
987       2
456       3

*表中还有多个其他列,但此特定数据需要表示为累积计数(当类型 = 依赖时(。

当值相同(例如依赖(并且多次反映时,我不确定如何处理。

你可以通过这种方式使用row_number(),你将得到 Type 的唯一行号:

select t.app_id, t.type, 
row_number() over (partition by t.type order by t.app_id) as rowno_type
from table t;

最新更新