ARRAY_AGG(STRUCT(x,y,z))等效于Bigquery遗留SQL

时间:2017-05-22 12:39:31

标签: google-bigquery legacy-sql

我有以下结构的标准SQL查询

SELECT a, ARRAY_AGG(STRUCT(x,y,z))
FROM t
GROUP BY a

如何在旧版SQL中编写相同的查询?

2 个答案:

答案 0 :(得分:2)

使用旧版SQL无法使用NEST非叶字段。唯一的解决方法是将x,y,z打包成一个字符串(例如构造JSON),然后在其上使用NEST,并且每当需要单独的字段时,使用一些字符串解析函数或Javascript UDF。不用说,使用标准SQL会更简单。

答案 1 :(得分:0)

同时,如果您仍需要在BigQuery Legacy SQL中使用它 - 请参阅下面的简单示例。

BigQuery标准SQL版

  
#standardSQL
WITH t AS (
  SELECT 1 AS a, 11 AS x, 12 AS y, 13 AS z UNION ALL
  SELECT 2 AS a, 21 AS x, 22 AS y, 23 AS z UNION ALL
  SELECT 3 AS a, 31 AS x, 32 AS y, 33 AS z
)
SELECT 
  a, ARRAY_AGG(STRUCT(x, y, z)) AS aa 
FROM t
GROUP BY a  

BigQuery Legacy SQL版本(确保设置目标表并关闭展平结果 - 否则UI会使输出变平)

#legacySQL
SELECT a, aa.*
FROM JS( 
  ( // input table 
  SELECT 
    a, GROUP_CONCAT(CONCAT(STRING(x), ';', STRING(y), ';', STRING(z))) AS aa 
  FROM 
  (SELECT 1 AS a, 11 AS x, 12 AS y, 13 AS z),
  (SELECT 2 AS a, 21 AS x, 22 AS y, 23 AS z),
  (SELECT 3 AS a, 31 AS x, 32 AS y, 33 AS z)
  GROUP BY a
  ), 
  a, aa, // input columns 
  "[ // output schema 
  {name: 'a', type:'integer'},
  {name: 'aa', type:'record', mode:'repeated', 
  fields: [
    {name: 'x', type: 'integer'},
    {name: 'y', type: 'integer'},
    {name: 'z', type: 'integer'}
    ]}
   ]", 
  "function(row, emit) { // function 
    var aa = []; 
    aa1 = row.aa.split(',');
    for (var i = 0; i < aa1.length; i++) { 
      aa2 = aa1[i].split(';');
      aa.push({x:parseInt(aa2[0]), y:parseInt(aa2[1]), z:parseInt(aa2[2])}); 
    }; 
    emit({
      a: row.a, 
      aa: aa
      }); 
  }"
)