簡體   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