0

I am trying to create a pivot sort of thing for a group of customer ids (cids).The dataset is of the form

Cid Match
A 1
A 2
B 2
B 3
B 4

The transformed dataset should look like

Cid Match_1 Match_2 Match_3
A 1 1 null
B 2 3 4

Here the Match_n suffix is dynamic and should be decided based on the max rows present across all Cids. I am not sure if this is even feasible.

I have tried pivot and other things but eventually hit a wall.

Found this but this is for a specific range of i (Big Query Transpose).

P.S: I just thought of something! Maybe that works. But still currently need help.

Aleksey Tsalolikhin
  • 1,518
  • 7
  • 14
  • please share all the code that you have tried – Kirby Apr 25 '23 at 23:13
  • `Match_2` at first row in your expected output shouldn't be `2` by any chance ? And order of column value is significant ? I mean For `B`, `3, 2, 4` or `4, 3, 2` is possible output? or you're just expecting them sorted ? – Jaytiger Apr 26 '23 at 01:04

1 Answers1

0

Match_n suffix is dynamic and should be decided based on the max rows present across all Cids.

I think you should consider a dynamic SQL along with PIVOT query.

-- sample data
CREATE TEMP TABLE sample_table AS
SELECT Cid, Match 
  FROM UNNEST(SPLIT('AABBB','')) Cid WITH offset 
  JOIN UNNEST([1, 2, 2, 3, 4]) Match WITH offset USING(offset);

-- query starts here  
EXECUTE IMMEDIATE FORMAT("""
  SELECT * 
    FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY Cid ORDER BY Match) rn FROM sample_table)
   PIVOT (ANY_VALUE(Match) Match FOR rn IN (%s))
""", (
  SELECT STRING_AGG('' || n, ',') 
    FROM UNNEST(GENERATE_ARRAY(
                  1,
                  (SELECT COUNT(1) FROM sample_table 
                    GROUP BY Cid ORDER BY 1 DESC LIMIT 1)
               )) n
  ));

Query Result

enter image description here

Jaytiger
  • 11,626
  • 2
  • 5
  • 15