计算每个值的重复项

Luc*_*tti 2 postgresql

我需要在我的表中添加一列,其中包含每次出现的次数。因此,如果值是唯一的,则列值应为 1,但如果有多个副本,我需要为每一行 (1,2...) 设置一个不同的值

例如,数据库是 postgres

ID 价值 数数
1 一种 1
2 一种 2
3 1
4 2
5 3
6 C 1

数据库示例https://dbfiddle.uk/?rdbms=postgres_10&fiddle=0e7132f3f094bd93b390ccae2e811696

a_h*_*ame 5

这可以使用窗口函数完成

select id, value, count(*) over (partition by value order by id) 
from the_table
order by id, value;
Run Code Online (Sandbox Code Playgroud)