Search code examples
postgresqlgroup-byrow-number

How to assign a unique identifier to the first n rows of a query in postgresql?


In postgres SQL, how can I assign a unique identifier (rn?) to the first n rows of a query and a unique identifier to the n subsequent rows? The objective is to group by ticker and unique identifier, see actual vs expected screenshots for details.

Actual query

SELECT 
    *,
    SUM(eps_diluted) OVER (PARTITION BY ticker ORDER BY rn ASC ROWS BETWEEN CURRENT ROW AND 3 FOLLOWING) AS test_eps_diluted
FROM(
    SELECT 
        "PK",
        ticker,
        period_end_date,
        eps_diluted,
        ROW_NUMBER() OVER ( PARTITION BY ticker ORDER BY period_end_date DESC ) rn
    FROM "ANALYTICS"."vQUARTERLY_MASTER_MATERIALIZED" 
    --WHERE ticker = 'ACN' 
    ORDER BY ticker, period_end_date DESC
    ) q
ORDER BY ticker, period_end_date desc 

Actual result

Expected result


Solution

  • I found a workaround that works using a modulo. This solution allows me to SUM eps_diluted for the last most recent quarters, the subsequent 4 quarters, and so on. See Solution screenshot for details.

    SELECT 
        * 
    FROM (
        SELECT 
            *,
            SUM(eps_diluted) OVER (PARTITION BY ticker ORDER BY rn ASC ROWS BETWEEN CURRENT ROW AND 3 FOLLOWING) AS sum_eps_diluted,
            1+ ((rn - 1) % 4) AS modulo
        FROM(
            SELECT 
                "PK",
                ticker,
                period_end_date,
                eps_diluted,
                ROW_NUMBER() OVER ( PARTITION BY ticker ORDER BY period_end_date DESC ROWS BETWEEN CURRENT ROW AND 3 FOLLOWING) rn
            FROM "ANALYTICS"."vQUARTERLY_MASTER_MATERIALIZED" 
            ORDER BY ticker, period_end_date DESC
            ) t1
        --WHERE 1+ ((rn - 1) % 4) = 1
        ORDER BY ticker, period_end_date desc ) t2
    WHERE modulo = 1
    

    Solution