Bigquery:如何声明数组变量并使用 select 语句设置数据?
Bigquery : how to declare an array variable and set data with a select statement?
我试图在 BigQuery 上声明一个数组变量,但我没能在我的变量中放入 SQL 语句。我找不到关于那个的任何话题。
我想将 table 的所有列名称放入我的变量中,所以我尝试了这个:
DECLARE my_array ARRAY <STRING>;
SET my_array = (
SELECT column_name
FROM my_project.my_dataset.INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'my_table'
);
SELECT my_array
我想我有语法问题,因为错误是:
Query error: Cannot coerce expression (
SELECT column_name
FROM my_project.my_dataset.INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'my_table'
) to type ARRAY<STRING> at [3:16]
谢谢,
你要试试这个吗?
DECLARE my_array ARRAY <STRING>;
SET my_array = ARRAY(
SELECT column_name
FROM my_project.my_dataset.INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'my_table'
);
SELECT my_array;
或者,这也可以。
SET my_array = (
SELECT ARRAY_AGG(column_name)
FROM my_project.my_dataset.INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'my_table'
);
此类查询的输出不是作为声明变量的数组,因此您必须将结果聚合为数组以匹配正确的类型:
DECLARE something ARRAY<STRING>;
SET something = (
SELECT ARRAY_AGG(column_name)
FROM my_project.my_dataset.INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'my_table'
);
SELECT something;
您可以找到文档 at this page
可以将字符串数组用作 #1
,但您也可以使用方法 #2
来简单地 return a table with table_name
and column_name
.
# 1) 数组
DECLARE colum_name_array ARRAY <STRING>;
SET colum_name_array = (
SELECT ARRAY_AGG(column_name)
FROM `[project_id].[datset_name].INFORMATION_SCHEMA.COLUMNS`
WHERE table_name = '[table_name]'
);
SELECT colum_name_array;
# 2) Table 带列名称的名称(未嵌套)
WITH
table_columns AS (
SELECT table_name, ARRAY_AGG(column_name) as column_name_array
FROM `[project_id].[datset_name].INFORMATION_SCHEMA.COLUMNS`
WHERE table_name = '[table_name]'
)
SELECT table_name, colum_name
FROM table_columns, UNNEST(column_name_array) as colum_name
;
我试图在 BigQuery 上声明一个数组变量,但我没能在我的变量中放入 SQL 语句。我找不到关于那个的任何话题。
我想将 table 的所有列名称放入我的变量中,所以我尝试了这个:
DECLARE my_array ARRAY <STRING>;
SET my_array = (
SELECT column_name
FROM my_project.my_dataset.INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'my_table'
);
SELECT my_array
我想我有语法问题,因为错误是:
Query error: Cannot coerce expression (
SELECT column_name
FROM my_project.my_dataset.INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'my_table'
) to type ARRAY<STRING> at [3:16]
谢谢,
你要试试这个吗?
DECLARE my_array ARRAY <STRING>;
SET my_array = ARRAY(
SELECT column_name
FROM my_project.my_dataset.INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'my_table'
);
SELECT my_array;
或者,这也可以。
SET my_array = (
SELECT ARRAY_AGG(column_name)
FROM my_project.my_dataset.INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'my_table'
);
此类查询的输出不是作为声明变量的数组,因此您必须将结果聚合为数组以匹配正确的类型:
DECLARE something ARRAY<STRING>;
SET something = (
SELECT ARRAY_AGG(column_name)
FROM my_project.my_dataset.INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'my_table'
);
SELECT something;
您可以找到文档 at this page
可以将字符串数组用作 #1
,但您也可以使用方法 #2
来简单地 return a table with table_name
and column_name
.
# 1) 数组
DECLARE colum_name_array ARRAY <STRING>;
SET colum_name_array = (
SELECT ARRAY_AGG(column_name)
FROM `[project_id].[datset_name].INFORMATION_SCHEMA.COLUMNS`
WHERE table_name = '[table_name]'
);
SELECT colum_name_array;
# 2) Table 带列名称的名称(未嵌套)
WITH
table_columns AS (
SELECT table_name, ARRAY_AGG(column_name) as column_name_array
FROM `[project_id].[datset_name].INFORMATION_SCHEMA.COLUMNS`
WHERE table_name = '[table_name]'
)
SELECT table_name, colum_name
FROM table_columns, UNNEST(column_name_array) as colum_name
;