BigQuery只需一次查询即可创建小型示例表

时间:2019-09-26 16:33:17

标签: sql google-bigquery

我知道BigQuery现在支持CREATE TABLE ... AS SELECT...之类的标准SQL语法。但是,没有人有提供一些示例数据来生成此表的示例吗?

说我想要两列分别为 A B A 值应为1,2,3,4 B 值应为"Alpha", "Bravo", "Charlie", "Delta"。如何生成这样的查询并创建这样的表?我意识到这对于创建大表不是很有效,但是想知道创建小表的推荐方法。

2 个答案:

答案 0 :(得分:1)

select . . . union all怎么样?

create table t as
    select 1 as a, 'Alpha' as b union all
    select 2 as a, 'Bravo' as b union all
    select 3 as a, 'Charlie' as b union all
    select 4 as a, 'Delta' as b;

答案 1 :(得分:1)

另一个不太繁琐的选择是

CREATE TABLE `PROJECT_ID.DATASET_NAME.NEW_TABLE_NAME` AS  
SELECT * FROM UNNEST([
  STRUCT(1 AS a, 'Alpha' AS b),
  (2, 'Bravo'),
  (3, 'Charlie'),
  (4, 'Delta')
])   

或使其更易于与现有的CSV重复使用

CREATE TABLE `PROJECT_ID.DATASET_NAME.NEW_TABLE_NAME` AS  
SELECT * FROM UNNEST([
  STRUCT(NULL AS a, '' AS b),
  (1, 'Alpha'),
  (2, 'Bravo'),
  (3, 'Charlie'),
  (4, 'Delta')
])
WHERE NOT a IS NULL