bigquery旋转嵌套字段

时间:2019-11-01 22:30:43

标签: sql google-bigquery

我有一个带架构的bigquery表:

enter image description here

我想得到这个结果:

flow_timestamp, channel_name, number_of_digits
2019-10-31 15:31:15, channel_name_1, 3,
2019-10-31 15:31:15, channel_name_2, 4,
:
:

我的查询:SELECT flow_timestamp, timeseries.channel_name, MAX(IF(channel_properties.key = 'number_of_digits', channel_properties.value, NULL)) AS number_of_digits FROM my_table , unnest(timeseries.channel_properties) as channel_properties

我尝试了此处显示的相同技术, How to aggregate multiple rows into one in BigQuery?

但出现错误SELECT list expression references column flow_timestamp which is neither grouped nor aggregated at [1:8]

1 个答案:

答案 0 :(得分:1)

以下是用于BigQuery标准SQL

#standardSQL
SELECT 
  flow_timestamp, 
  timeseries.channel_name, 
  ( SELECT MAX(IF(channel_properties.key = 'number_of_digits', channel_properties.value, NULL)) 
    FROM UNNEST(timeseries.channel_properties) AS channel_properties
  ) AS number_of_digits 
FROM my_table