sqldatabricks

Explode the rows based on the sum of another column


I have a table T1 contains two rows and I want to split the rows based on the total numbers of cat_ids.

CREATE OR REPLACE TEMP VIEW T1 AS
SELECT 'ISAC' AS CAT_ID, 'STC-5-DM' AS CAT_CD, 3 AS CAT_QTY
UNION ALL
SELECT 'ISAC', 'STC-15-DM', 2;

I have tried this but it is not giving me expected result.

 WITH TotalQty AS (
    SELECT CAT_ID, CAT_CD, CAT_QTY,
           SUM(CAT_QTY) OVER (PARTITION BY CAT_ID) AS total_qty
    FROM T1
),
ExpandedRows AS (
    SELECT CAT_ID, CAT_CD, 
           EXPLODE(SEQUENCE(1, (SELECT MAX(total_qty) FROM TotalQty))) AS CAT_QTY
    FROM TotalQty
)
SELECT CAT_ID, CAT_CD, CAT_QTY
FROM ExpandedRows
ORDER BY CAT_ID, CAT_QTY;

expected output is

CAT_ID  CAT_CD  CAT_QTY
ISAC    STC-5-DM    1
ISAC    STC-5-DM    2
ISAC    STC-5-DM    3
ISAC    STC-15-DM   4
ISAC    STC-15-DM   5

Solution

  • You could use a recursive cte to fetch all the rows, and then the row_number to show the numbers

    -- define the cte
    with cats_cte(cat_id, cat_cd, cat_qty) as
    (
      -- this is the original query, the start query
      select cat_id, cat_cd, cat_qty
      from   T1
    
      union all
    
      -- this is the recursive query, it queries the cte that is in itself
      select cat_id, cat_cd, cat_qty - 1
      from   cats_cte
      where  cat_qty > 1 -- this makes the recursion stop
    )
    -- here we select results from the cte
    select c.cat_id, 
           c.cat_cd,
           row_number() over (partition by cat_id order by cat_cd desc, cat_qty)  as cat_cty
    from   cats_cte c
    
    order by cat_cd desc, cat_qty
    

    here is the dbFiddle

    the result is

    cat_id cat_cd cat_cty
    ISAC STC-5-DM 1
    ISAC STC-5-DM 2
    ISAC STC-5-DM 3
    ISAC STC-15-DM 4
    ISAC STC-15-DM 5