2016-12-17 56 views
1

我有三個BigQuery表格如下圖所示bq命令行工具 - 如何插入具有嵌套字段的Big查詢表?

員工

Employee_id | Department_id | Location_id | Name | Age 

Department_id | Department_Name | Department_Code 

位置

Location_id | Country | State | City 

下面的查詢用於連接所有三個表,

SELECT 
    e.Employee_id, 
    e.Name, 
    e.Age, 
    e.Department_id, 
    d.Department_Name, 
    d.Department_Code, 
    l.Location_id, 
    l.Country , 
    l.State, 
    l.City 
FROM Employee e 
JOIN Department d 
    ON e.Department_id = d. Department_id 
JOIN Location l 
    ON e.Location_id = l.Location_id 

如何插入這個結果集到一具有以下,使用BQ命令行實用程序(BQ查詢命令)嵌套字段架構BigQuery資料表?

enter image description here

回答

1

如何插入這個結果集到一具有以下嵌套字段架構BigQuery資料表?

下面是BigQuery Standard SQL

SELECT 
    e.Employee_id, 
    e.Name, 
    e.Age, 
    STRUCT<Department_id STRING, Department_Name STRING, Department_Code STRING>(
        e.Department_id, d.Department_Name, d.Department_Code) AS Department, 
    STRUCT<Location_id STRING, Country STRING, State STRING, City STRING>(
           l.Location_id, l.Country, l.State, l.City) AS Location 
FROM Employee e 
JOIN Department d 
    ON e.Department_id = d. Department_id 
JOIN Location l 
    ON e.Location_id = l.Location_id 

... BQ使用命令行實用程序(BQ查詢命令)?

bq query --use_legacy_sql=false --append_table --destination_table 'dataset.table' '**`above query`**'

看到herebq-command-line-tool更多細節

+0

謝謝。按預期工作。但如何在查詢中表示嵌套字段中的多層次(嵌套字段中的嵌套記錄)? – Vetri

+0

解決了我自己。應如下所示, 'SELECT FIELD1, FIELD2, STRUCT > ( 「PARENT_ID」,(1, 「名稱」)) FROM Your_Table ' – Vetri

+0

傳統SQL中不支持Struct。如何在傳統SQL中實現相同? – Vetri