2017-06-05 63 views
0

我如何可以別名字段1爲指標& &場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] 

非常感謝您的時間在應對

乾杯!

回答

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 

或下方可能會更「可讀」

#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 
+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是否接受它。驗證器爲綠色後,我繼續添加更多值。如果您嘗試立即構建所有內容,您可能會發現這是一項頗具挑戰性的任務。

+0

謝謝,這幫助我在我原來的查詢迭代 –