问题
I have a BigQuery table like this:
Required Output is :
Note : The keys in Extended_property_key column are not fixed, it keeps on adding frequently. Hence the columns in Output will also keep on adding.
I need to build a Bigquery which can handle dynamic adding of columns in output query along with pivoting.
回答1:
Below is for BigQuery Standard SQL
EXECUTE IMMEDIATE '''
SELECT account_id, ''' || (
SELECT STRING_AGG(DISTINCT "MAX(IF(Extended_property_key = '" || Extended_property_key || "', Extended_property_value, NULL)) AS " || Extended_property_key)
FROM `project.dataset.table`
) || '''
FROM `project.dataset.table`
GROUP BY 1
ORDER BY 1
'''
If applied to sample data in your question - output is
Row account_id Key1 Key2 Key3
1 1 Value1 null null
2 7 null Value2 Value3
回答2:
Try:
CALL fhoffa.x.pivot(
'p.d.t1' # source table
, 'p.d.t2' # destination table
, ['ACCOUNT_ID'] # row_ids
, 'EXTENDED_PROPERTY_KEY' # pivot_col_name
, 'EXTENDED_PROPERTY_VALUE' # pivot_col_value
, 30 # max_columns
, 'ANY_VALUE' # aggregation
, '' # optional_limit
);
More details on how to pivot:
- https://towardsdatascience.com/easy-pivot-in-bigquery-one-step-5a1f13c6c710
来源:https://stackoverflow.com/questions/62562149/parametrized-table-name-and-column-name-in-standard-sql-bigquery