PostgreSQL
 sql >> Base de Dados >  >> RDS >> PostgreSQL

Atribuir o mesmo id a linhas com a mesma combinação de dados


Faça isso na hora da 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