2016-01-25 38 views
2

我正在使用大查詢,並且我想創建一個使用「記錄」類型列填充表的作業。 數據將填充查詢 - 所以我怎麼能寫一個查詢返回「記錄」類型的列。使用列類型創建表RECORD

謝謝!

+0

你有一個率低。重要的是,您必須使用投票下方發佈答案左側的勾號標記接受的答案。這會增加你的速度。通過visinting這個鏈接看看這是如何工作的:http://meta.stackoverflow.com/questions/5234/how-does-accepting-an-answer-work#5235 – Pentium10

回答

2

不知何故,Pentium10提出的選項在GBQ UI或API Explorer中從未爲我工作過。
我可能失去了一些東西

與此同時,我找到了解決方法是在下面的例子

SELECT location.state, location.city FROM JS(
    (  // input table 
    SELECT NEST(CONCAT(state, ',', city)) AS locations 
    FROM (
    SELECT state, city FROM 
    (SELECT 'florida' AS state, 'miami' AS city), 
    (SELECT 'california' AS state, 'la' AS city), 
    (SELECT 'romania' AS state, 'transylvania' AS city) 
    ) 
), 
    locations,  // input columns 
    "[ // output schema 
    {'name': 'location', 'type': 'RECORD', 
    'mode': 'REPEATED', 
    'fields': [ 
     {'name': 'state', 'type': 'STRING'}, 
     {'name': 'city', 'type': 'STRING'} 
    ]  
    } 
    ]", 
    "function(row, emit){ // function 
    for (var i = 0; i < row.locations.length; i++) { 
     var c = []; 
     x = row.locations[i].split(','); 
     t = {state:x[0], city:x[1]} 
     c.push(t); 
     emit({location: c}); 
    }; 
    }" 
) 

請注意:
你應該設定目標表與Allow Large Results和未選中Flatten Results

結果輸出表是(在JSON模式下)

[ 
    { 
    "location": [ 
     { 
     "state": "california", 
     "city": "la" 
     } 
    ] 
    }, 
    { 
    "location": [ 
     { 
     "state": "florida", 
     "city": "miami" 
     } 
    ] 
    }, 
    { 
    "location": [ 
     { 
     "state": "romania", 
     "city": "transylvania" 
     } 
    ] 
    } 
] 

加入到解決一些問題@AdiCohen與他的真實的例子, 他在最近的評論表明:

問:我有查詢等欄目除了記錄列,但是當我跑 查詢,他們返回爲null。我怎樣才能創建一個與 類型的表?這裏

SELECT amount, currency, location.state, location.city FROM JS( 
    (// input table 
    SELECT NEST(CONCAT(state, ',', city)) AS locations, 
     SUM(amount) AS amount, MAX(currency) as currency 
    FROM ( 
     SELECT state, city, amount, currency, ROW_NUMBER() OVER() as grp FROM 
     (SELECT 'florida' AS state, 'miami' AS city, 'coins' AS currency, 40 AS amount), 
     (SELECT 'california' AS state, 'la' AS city, 'coins' AS currency, 40 AS amount), 
     (SELECT 'romania' AS state, 'transylvania' AS city,'coins' AS currency, 40 AS amount) 
    ) GROUP BY grp 
), 
    amount, currency, locations, // input columns 
    "[ // output schema 
    {'name': 'location', 'type': 'RECORD', 'mode': 'REPEATED', 
    'fields': [ 
     {'name': 'state', 'type': 'STRING'}, 
     {'name': 'city', 'type': 'STRING'} 
    ] }, 
    { 'name': 'amount', 'type': 'INTEGER'}, 
    { 'name': 'currency', 'type': 'STRING'} 
    ]", 
    "function(row, emit) { // function 
    for (var i = 0; i < row.locations.length; i++) { 
     var c = []; 
     x = row.locations[i].split(','); 
     t = {state:x[0], city:x[1]} 
     c.push(t); 
     emit({amount: row.amount, currency: row.currency, location: c}); 
    }; 
    }" 
) 

輸出是:

[ 
    { 
    "amount": "40", 
    "currency": "coins", 
    "location_state": "romania", 
    "location_city": "transylvania" 
    }, 
    { 
    "amount": "40", 
    "currency": "coins", 
    "location_state": "florida", 
    "location_city": "miami" 
    }, 
    { 
    "amount": "40", 
    "currency": "coins", 
    "location_state": "california", 
    "location_city": "la" 
    } 
] 
+0

非常感謝!它確實有幫助。我在哪裏可以找到關於udf功能的更多內容? –

+0

https://cloud.google.com/bigquery/user-defined-functions –

+0

重要的是,您可以使用投票下方發佈的答案左側的勾號標記接受的答案。請參閱http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work#5235爲什麼它很重要 –

1

您需要使用dot符號,以反映輸出作爲RECORD例如查詢:

select 
    'florida' as country.state, 
    'SFO' as country.city; 

在這個例子中country是記錄和state|city都在記錄中的字段。

+0

請澄清 - 這應該與BQ UI一起工作嗎?或API資源管理器 - https://cloud.google.com/bigquery/docs/reference/v2/jobs/insert#try-it?以上提議從未在BQ UI中爲我工作!你能舉例說明使用API​​ Explorer的工作設置嗎?會非常有幫助! –

+0

這個答案直接來自@MoshaPasumansky的支付谷歌支持誰提供了我們發佈幾個月前的票的答案 – Pentium10

+0

所以,看起來它不起作用?你有沒有嘗試過? –