如何在 BigQuery 中编写一个查询,该查询将从 SELECT 语句中的另一个 table 中插入一个模式?
How to write a query in BigQuery that will INSERT a schema in from another table from within SELECT statement?
[BIGQUERY/SQL 写作非常新手]
嗨,
我在 bigquery 中使用这个 dataset/query:
dataset/query
select * from fh-bigquery.reddit.subreddits limit 10;
我被要求编写一个查询,该查询将使用上面的 table 插入一个模式,它将在新的 table 中具有以下 JSON 模式结构:
schema: {
fields: [
{
mode: NULLABLE,
name: dt,
type: DATE
},
{
mode: NULLABLE,
name: num_comments,
type: INTEGER
},
{
mode: NULLABLE,
name: posts,
type: INTEGER
},
{
mode: NULLABLE,
name: ups,
type: INTEGER
},
{
mode: NULLABLE,
name: downs,
type: INTEGER
},
{
fields: [
{
mode: NULLABLE,
name: ups,
type: INTEGER
},
{
mode: NULLABLE,
name: downs,
type: INTEGER
}
],
mode: REPEATED,
name: subreddit_metrics,
type: RECORD
}
]
},
subreddit_metrics
字段按照上述 JSON 嵌套。
此查询来自 BigQuery 文档,它向我展示了如何为 table:
创建嵌套字段
CREATE TABLE IF NOT EXISTS mydataset.mytable(
id STRING,
first_name STRING,
last_name STRING,
dob DATE,
addresses
ARRAY<
STRUCT<
status STRING,
address STRING,
city STRING,
state STRING,
zip STRING,
numberOfYears STRING>>)
OPTIONS (description = 'Example name and addresses table')
根据最初的请求,要编写一个查询以根据上面的原始 dataset/query 插入架构,我无法从 SELECT 语句中创建嵌套字段来创建新的 table 与嵌套字段。像这样:
CREATE TABLE
mydataset.test AS
SELECT
subreddit ARRAY< STRUCT< ups STRING,
downs STRING,
FROM
fh-bigquery.reddit.subreddits;
Error: Syntax error: Expected end of input but got keyword ARRAY at [12:15]
问题:
1. Am I understanding the question correctly, in regards to writing a query that will INSERT a schema from another table in question?
2. If my understanding of #1 is correct, how can I INSERT a schema from another table,with the right nesting, I would think using a CREATE statement with the help from a SELECT statement, right?
提前谢谢你。
以下是我最好的问题。 subreddit_metrics
可以通过 ARRAY_AGG(STRUCT(ups, downs))
.
创建架构
SELECT subr AS subreddit,
created_utc AS dt,
num_comments,
c_posts AS posts,
ups,
downs,
ARRAY_AGG(STRUCT(ups, downs)) OVER(PARTITION BY subr) AS subreddit_metrics
FROM `fh-bigquery.reddit.subreddits`
;
[BIGQUERY/SQL 写作非常新手]
嗨,
我在 bigquery 中使用这个 dataset/query:
dataset/query
select * from fh-bigquery.reddit.subreddits limit 10;
我被要求编写一个查询,该查询将使用上面的 table 插入一个模式,它将在新的 table 中具有以下 JSON 模式结构:
schema: {
fields: [
{
mode: NULLABLE,
name: dt,
type: DATE
},
{
mode: NULLABLE,
name: num_comments,
type: INTEGER
},
{
mode: NULLABLE,
name: posts,
type: INTEGER
},
{
mode: NULLABLE,
name: ups,
type: INTEGER
},
{
mode: NULLABLE,
name: downs,
type: INTEGER
},
{
fields: [
{
mode: NULLABLE,
name: ups,
type: INTEGER
},
{
mode: NULLABLE,
name: downs,
type: INTEGER
}
],
mode: REPEATED,
name: subreddit_metrics,
type: RECORD
}
]
},
subreddit_metrics
字段按照上述 JSON 嵌套。
此查询来自 BigQuery 文档,它向我展示了如何为 table:
创建嵌套字段CREATE TABLE IF NOT EXISTS mydataset.mytable(
id STRING,
first_name STRING,
last_name STRING,
dob DATE,
addresses
ARRAY<
STRUCT<
status STRING,
address STRING,
city STRING,
state STRING,
zip STRING,
numberOfYears STRING>>)
OPTIONS (description = 'Example name and addresses table')
根据最初的请求,要编写一个查询以根据上面的原始 dataset/query 插入架构,我无法从 SELECT 语句中创建嵌套字段来创建新的 table 与嵌套字段。像这样:
CREATE TABLE
mydataset.test AS
SELECT
subreddit ARRAY< STRUCT< ups STRING,
downs STRING,
FROM
fh-bigquery.reddit.subreddits;
Error: Syntax error: Expected end of input but got keyword ARRAY at [12:15]
问题:
1. Am I understanding the question correctly, in regards to writing a query that will INSERT a schema from another table in question?
2. If my understanding of #1 is correct, how can I INSERT a schema from another table,with the right nesting, I would think using a CREATE statement with the help from a SELECT statement, right?
提前谢谢你。
以下是我最好的问题。 subreddit_metrics
可以通过 ARRAY_AGG(STRUCT(ups, downs))
.
SELECT subr AS subreddit,
created_utc AS dt,
num_comments,
c_posts AS posts,
ups,
downs,
ARRAY_AGG(STRUCT(ups, downs)) OVER(PARTITION BY subr) AS subreddit_metrics
FROM `fh-bigquery.reddit.subreddits`
;