如何使用row_number()为postgresql中的重复值分配行号

如何使用row_number()为postgresql中的重复值分配行号,postgresql,Postgresql,我在数据中有一些行,其中agentid和客户ID被重复。像这样 我想为每个重复的值分配一个行号,以便结果如下表所示:表2: 阿根蒂德 客户ID 行号 26 1234 1. 26 1234 2. 26 1234 3. 26 1234 4. 26 1454 1. 26 1256 1. 26 1256 2. 30 1256 1. 你想要: WITH cte AS ( SELECT *, ROW_NUMBER() OVER (PARTITION BY agentid, customerid O

我在数据中有一些行,其中agentid和客户ID被重复。像这样

我想为每个重复的值分配一个行号,以便结果如下表所示:表2:

阿根蒂德 客户ID 行号 26 1234 1. 26 1234 2. 26 1234 3. 26 1234 4. 26 1454 1. 26 1256 1. 26 1256 2. 30 1256 1. 你想要:

WITH cte AS (
    SELECT *, ROW_NUMBER() OVER (PARTITION BY agentid, customerid ORDER BY random()) rn,
              COUNT(*) OVER (PARTITION BY agentid, customerid) cnt
    FROM aa_dev.calls
)

SELECT agentid, customerid, rn      
FROM cte
WHERE cnt > 1;
你想要:

WITH cte AS (
    SELECT *, ROW_NUMBER() OVER (PARTITION BY agentid, customerid ORDER BY random()) rn,
              COUNT(*) OVER (PARTITION BY agentid, customerid) cnt
    FROM aa_dev.calls
)

SELECT agentid, customerid, rn      
FROM cte
WHERE cnt > 1;