Facing issue with Dynamic Unpivot in Google BigQuery

Viewed 37

Desired Data

Original Data

I am using the script below to achieve this:

DECLARE myunpivot STRING;
SET myunpivot = (
  SELECT CONCAT('(', STRING_AGG( column_name, ','), ')'),
From(
SELECT column_name FROM `ProjectA.ProjectA1.INFORMATION_SCHEMA.COLUMNS`
where table_name ="my_table" 
));


EXECUTE IMMEDIATE format("""

SELECT * FROM `ProjectA.ProjectA1.table_name`
unpivot
(
  columns 
  FOR value in %s
)
""", myunpivot);

I am getting this error- "Unexpected Keyword DECLARE" although I ensured that DECLARE statement is the first line in my code.

On removing the DECLARE statement, I am getting this error- "Executing OTHER_STATEMENT statements is not implemented"

I am not able to figure out what the issue is over here. Please Help!

0 Answers
Related