简体   繁体   English

在 BigQuery 中使用列名创建数组

[英]Create array with column names in BigQuery

I have a table with multiple columns.我有一个包含多列的表。 I want to create an array that contains all names of the columns that appear in the table, such that I can work with this array later.我想创建一个数组,其中包含出现在表中的所有列的名称,以便以后可以使用该数组。

I selected the column names using this query:我使用此查询选择了列名:

SELECT column_name FROM Books1.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'table1'

Now I want to declare and set an array with the column names:现在我想声明并设置一个包含列名的数组:

DECLARE column_names ARRAY <STRING>
SET column_names = **

I don't know with what lines of code should I replace **.我不知道应该用哪几行代码替换**。 On the inte.net I only found examples where I can create an array with a predefined set of values.在 inte.net 上,我只找到了可以创建具有一组预定义值的数组的示例。

Can anyone help?谁能帮忙?

You should be able to achieve what you want with the following:您应该能够通过以下方式实现您想要的目标:

DECLARE column_names ARRAY <STRING>
SET column_names = (SELECT arraY_agg(column_name) FROM Books1.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'table1'
);

Adding array_agg makes the query return an array of the column names.添加array_agg使查询返回列名数组。

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

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