2016-01-25 39 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

所以,看起来它不起作用?你有没有尝试过? –