繁体   English   中英

如何在 BigQuery 中编写一个查询,该查询将从 SELECT 语句中的另一个表中插入架构?

[英]How to write a query in BigQuery that will INSERT a schema in from another table from within SELECT statement?

[BIGQUERY/SQL 编写的新手]

你好,

我在bigquery中使用这个数据集/查询:

数据集/查询

  select * from fh-bigquery.reddit.subreddits limit 10;

我被要求编写一个查询,该查询将使用上表插入模式,新表中将具有以下 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 文档,它向我展示了如何为表创建嵌套字段:

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')

根据原始请求,要编写一个基于上述原始数据集/查询插入模式的查询,我无法从 SELECT 语句中创建嵌套字段来创建具有嵌套字段的新表。 像这样:

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`
;  

暂无
暂无

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

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