sql >> Base de Datos >  >> RDS >> PostgreSQL

Asigne la misma identificación a las filas con la misma combinación de datos

Hazlo en el momento de la consulta:

with t (year, folder, seq_no) as (values
    (1973,5,11),
    (2010,4,7),
    (1973,11,12),
    (1973,5,11),
    (1500,4,4),
    (2010,4,7)
)
select
    dense_rank() over (order by year, folder, seq_no) as id,
    year, folder, seq_no,
    count(*) over (partition by year, folder, seq_no) as "count"
from t
;
 id | year | folder | seq_no | count 
----+------+--------+--------+-------
  1 | 1500 |      4 |      4 |     1
  2 | 1973 |      5 |     11 |     2
  2 | 1973 |      5 |     11 |     2
  3 | 1973 |     11 |     12 |     1
  4 | 2010 |      4 |      7 |     2
  4 | 2010 |      4 |      7 |     2