Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
184 views
in Technique[技术] by (71.8m points)

sql - Pivot in BigQuery

When i use pivot function in BigQuery, something as snippet below

CALL fhoffa.x.pivot(
  'bigquery-public-data.ghcn_d.ghcnd_2019' # source table
  , 'fh-bigquery.temp.test_pivotted' # destination table
  , ['id', 'date'] # row_ids
  , 'element' # pivot_col_name
  , 'value' # pivot_col_value
  , 30 # max_columns
  , 'AVG' # aggregation
  , 'LIMIT 10' # optional_limit
);

The pivoted column name has a prefix e_

id date e_PRCP e_TMIN e_TMAX e_SNOW
1 27-01-2021 1 2 5 8
2 28-01-2021 3 5 9 3
3 29-01-2021 5 7 4 1
question from:https://stackoverflow.com/questions/65948834/pivot-in-bigquery

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here's full definition of the procedure, you can create one in your own dataset and modify as you see fit. You can see the e_ was added inside the first EXECUTE IMMEDIATE.

CREATE OR REPLACE PROCEDURE `fhoffa.x.pivot`(table_name STRING, destination_table STRING, row_ids ARRAY<STRING>, pivot_col_name STRING, pivot_col_value STRING, max_columns INT64, aggregation STRING, optional_limit STRING)
BEGIN
  DECLARE pivotter STRING;
EXECUTE IMMEDIATE (
    "SELECT STRING_AGG(' "||aggregation
    ||"""(IF('||@pivot_col_name||'="'||x.value||'", '||@pivot_col_value||', null)) e_'||fhoffa.x.normalize_col_name(x.value))
   FROM UNNEST((
       SELECT APPROX_TOP_COUNT("""||pivot_col_name||", @max_columns) FROM `"||table_name||"`)) x"
  ) INTO pivotter 
  USING pivot_col_name AS pivot_col_name, pivot_col_value AS pivot_col_value, max_columns AS max_columns;
EXECUTE IMMEDIATE (
   'CREATE OR REPLACE TABLE `'||destination_table
   ||'` AS SELECT '
   ||(SELECT STRING_AGG(x) FROM UNNEST(row_ids) x)
   ||', '||pivotter
   ||' FROM `'||table_name||'` GROUP BY '
   || (SELECT STRING_AGG(''||(i+1)) FROM UNNEST(row_ids) WITH OFFSET i)||' ORDER BY '
   || (SELECT STRING_AGG(''||(i+1)) FROM UNNEST(row_ids) WITH OFFSET i)
   ||' '||optional_limit
  );
END;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...