'Query table with unpredictable number of columns by BigQuery

Let's say, I have a table from log, it looks like this: Log Table

I need a result table like: Result table

But the problem is my dataset is not only 7, but maybe 24, 100 value columns. My query with 7 value columns is:

select 
*
from My_Dataset
unpivot 
(status for value in (value_1, value_2, value_3, value_4, value_5, value_6, value_7))```

But is there anyway to automatic this process for value_n?
Thank you.


Solution 1:[1]

It's possible via SQL scripting.

You have to get a column list of your table first and save it in variable. Then call dynamic query with EXECUTE IMMEDIATE

DECLARE field_list STRING;

SET field_list = ((
    SELECT STRING_AGG(column_name) FROM `my_project_id`.my_dataset.INFORMATION_SCHEMA.COLUMNS
    WHERE column_name LIKE "value_%" AND table_name = 'my_table'
));

EXECUTE IMMEDIATE "SELECT id, SPLIT(value, '_')[OFFSET(1)] value FROM  my_dataset.my_table UNPIVOT (status FOR value IN ("||field_list||"))" 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Timogavk