简体   繁体   English

将数组的数组转换为多列 BigQuery SQL

[英]Convert Array of an Array to Multiple Columns BigQuery SQL

I am trying to convert an array of array to multiple columns.我正在尝试将数组数组转换为多列。 The data structure is as follows:数据结构如下:

Column name: Inputs
{
  answers: [{
    type: END_TIME,
    answer:   [{
      int_value: 1015
    }]
  },{
    type: LOCATION,
    answer:   [{
      string_value: "SAN_JOSE"
    },{
      string_value: "CA"
    }]
  }],
  username: "xxxxx",
  status: COMPLETE
}

Ideal output would be理想的输出是

end_time location       username  status
1015     SAN_JOSE, CA   xxxxx     COMPLETE

But I am getting the following result:但我得到以下结果:

end_time                 location                        username   status
[{                       [{                              xxxxx      COMPLETE
      int_value: 1015        string_value: "SAN_JOSE"
    }]                   },{
                             string_value: "CA"
                         }]

Using the sample statement below使用下面的示例语句

SELECT
  (SELECT answer FROM UNNEST(Inputs.answers) where type = 'END_TIME') end_time,
  (SELECT answer FROM UNNEST(Inputs.answers) where type = 'LOCATION') location,
  t.Inputs.username username,
  t.Inputs.status status
FROM table_name t
;

Any suggestions would be greatly appreciated.任何建议将不胜感激。 TIA! TIA!

Can you try this query:你能试试这个查询吗:

with table_name as(
select struct([struct("END_TIME" as type,["1015"] as answer),struct("LOCATION" as type,["SAN_JOSE","CA"] as answer)]as answers,
            "xxxxx"as username,
            "COMPLETE"as status)inputs
)
SELECT
 t.inputs.username,
 t.inputs.status
 ,answers.type
 ,array_to_string(answers.answer, ",")answer
 FROM table_name t , unnest(t.inputs.answers)answers
 
pivot(
 max(answer) for type in ("END_TIME","LOCATION")
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM