PostgreSQL
 sql >> Database >  >> RDS >> PostgreSQL

Assegna lo stesso ID alle righe con la stessa combinazione di dati

Fallo al momento della query:

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