简体   繁体   English

Bigquery 将一个表插入到另一个表中,同时使用“with”来提前声明变量

[英]Bigquery insert a table into another one while using "with" to declare variables ahead

I need to insert a dataset into another one and here is my snippet:我需要将一个数据集插入另一个数据集,这是我的片段:

With questions AS (
SELECT * FROM 
`bigquery-public-data.stackoverflow.posts_questions` A
JOIN
`bigquery-public-data.stackoverflow.users` U
ON A.owner_user_id = U.id
),

POST_USER_TAG_INFO AS (
SELECT Q.*, t.* FROM
questions Q CROSS JOIN 
unnest(split(Q.tags, '|')) col join
bigquery-public-data.stackoverflow.tags t
ON
col=t.tag_name
)

INSERT INTO inbound-summit-278521.datawarehousing_coursework.Dimensions (ID, post_id, user_id, tag_id, badge_id, location_id)
SELECT ROW_NUMBER() OVER(), P.id, owner_user_id, P.id_2, b.id, location FROM
POST_USER_TAG_INFO P
JOIN
bigquery-public-data.stackoverflow.badges b
ON P.owner_user_id = b.user_id
LIMIT 100;

It returns that there is a bug:它返回存在错误:

Syntax error: Expected "(" or "," or keyword SELECT but got keyword INSERT at [18:1]

NOTE : I am using Google Bigquery.注意:我正在使用 Google Bigquery。

The WITH clause goes into the SELECT branch of the INSERT ...SELECT : WITH子句进入 INSERT ...SELECT 的SELECT分支:


INSERT INTO inbound-summit-278521.datawarehousing_coursework.Dimensions (
  ID, post_id, user_id, tag_id, badge_id, location_id
)
WITH questions AS (
  SELECT * FROM 
  `bigquery-public-data.stackoverflow.posts_questions` A
  JOIN
  `bigquery-public-data.stackoverflow.users` U
  ON A.owner_user_id = U.id
),
POST_USER_TAG_INFO AS (
  SELECT Q.*, t.* FROM
  questions Q CROSS JOIN 
  unnest(split(Q.tags, '|')) col join
  bigquery-public-data.stackoverflow.tags t
  ON
  col=t.tag_name
)
SELECT ROW_NUMBER() OVER(), P.id, owner_user_id, P.id_2, b.id, location FROM
POST_USER_TAG_INFO P
JOIN
bigquery-public-data.stackoverflow.badges b
ON P.owner_user_id = b.user_id
LIMIT 100;

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

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