I spent some time to do a - I think - simply dinamic pivot on BigQuery (the columns will be added dynamicaly), but I could find a solution. Some tutorials I have tried:

  1. How to use dynamic pivot table in bigquery?
  2. PIVOTing BigQuery tables on DATE type
  3. https://www.youtube.com/watch?v=QWIGpplBUuQ

The guy in number 1 had the same error massage, but not able to find a solution. A piece of my code is like:

declare dec string;

set dec = (
  select        
   string_agg (distinct concat("'",extract (year from date1), '-', 
extract (month from date1), "'"   )) as ref_active_cte
  from` table
);

execute immediate
"""

with cte_block1 as (), -- does not really matter
cte_block2 as () -- -- does not really matter

select
  *
 from
 (
  select value, classes, year
 )
 pivot(sum(value) for classes in  (?)  )
"""
using dec

I just need a simple template code that I can use to make a dynamic pivot, because I will have many months or maybe classes. And I would like to know what I am doing wrong in my code.

0

There are 0 answers