别名嵌套结构列

时间:2017-06-05 19:29:24

标签: google-bigquery

我如何将field1别名为index& &安培;字段2为值

查询给出了一个错误:

#standardsql
with q1 as (select 1 x, ARRAY<struct<id string, cd ARRAY<STRUCT<index STRING,value STRING>>>>
                    [struct('h1',[('1','a')
                                 ,('2','b')
                                 ])
                          ,('h2',[('3','c')
                                 ,('4','d')
                                 ])] hits
       )


Select * from q1
ORDER by x

Error: Array element type STRUCT<STRING, ARRAY<STRUCT<STRING, STRING>>> does not coerce to STRUCT<id STRING, cd ARRAY<STRUCT<index STRING, value STRING>>> at [5:26]

非常感谢你的回复时间

干杯!

2 个答案:

答案 0 :(得分:1)

当我尝试使用标准版本在BigQuery中模拟数据时,我通常会尽可能地命名所有变量和别名。例如,如果你这样构建它,你的数据就会起作用:

with q1 as (
select 1 x, ARRAY<struct<id string, cd ARRAY<STRUCT<index STRING,value STRING>>>>  [struct('h1' as id,[STRUCT('1' as index,'a' as value) ,STRUCT('2' as index ,'b' as value)] as cd), STRUCT('h2',[STRUCT('3' as index,'c' as value) ,STRUCT('4' as index,'d' as value)] as cd)] hits
)

select * from q1
order by x

注意我已经构建了结构并在其中放置别名以使其工作(如果删除别名和结构,它可能不起作用,但我发现这似乎是相当间歇的。如果你完全描述变量然后它一直工作。)

另外,作为推荐,我尝试逐个构建模拟数据。首先,我创建结构并测试它以查看BigQuery是否接受它。验证器为绿色后,我继续添加更多值。如果您尝试一次构建所有内容,您可能会发现这是一项具有挑战性的任务。

答案 1 :(得分:1)

#standardsql
WITH q1 AS (
  SELECT 
    1 AS x, 
      [
        STRUCT('h1' AS id, [STRUCT('1' AS index, 'a' AS value), ('2','b')] AS cd), 
        STRUCT('h2', [STRUCT('3' AS index, 'c' AS value), ('4','d')] AS cd)
      ] AS hits
)
SELECT * 
FROM q1
-- ORDER BY x

或以下可能更多&#34;可读&#34;

#standardsql
WITH q1 AS (
  SELECT 
    1 AS x, 
      [
        STRUCT('h1' AS id, [STRUCT<index STRING, value STRING>('1', 'a'), ('2','b')] AS cd), 
        STRUCT('h2', [STRUCT<index STRING, value STRING>('3', 'c'), ('4','d')] AS cd)
      ] AS hits
)
SELECT * 
FROM q1
-- ORDER BY x