如何在 BigQuery 标准 SQL 中透视多个列

ple*_*ice 5 google-bigquery

我想在表中旋转多个指标。如何编写语法来实现以下功能?

当前表

date          iso_week          iso_year          metric1         metric2
2021-12-01       48              2021              1000             500
2021-11-30       48              2021               850             300
...
2020-11-28       48              2020               800             400
2020-11-27       48              2020               950             450
...
2019-11-27       48              2019               700             350
2019-11-26       48              2019               820             380
Run Code Online (Sandbox Code Playgroud)

期望的输出

iso_week          metric1_thisYear        metric1_prevYear       metric1_prev2Year       metric2_thisYear        metric2_prevYear        metric_prev2Year
48                     1850                   1750                   1520                   800                   950                   730
Run Code Online (Sandbox Code Playgroud)

...

Mik*_*ant 12

考虑以下方法

select * from (
  select * except(date)
  from your_table
)
pivot (sum(metric1) as metric1, sum(metric2) as metric2 for iso_year in (2021, 2020, 2019))        
Run Code Online (Sandbox Code Playgroud)

如果应用于您问题中的样本数据 - 输出是

在此输入图像描述