2017-10-19 74 views
0

我有一個嵌套表,我無法訪問所有使用標準谷歌BigQuery的字段。無法使用谷歌bigquery取消嵌套一些字段(標準)

例如查詢失敗

SELECT * 
FROM 
    (
    SELECT 
      rev_info.user.id as player_id, 
      rev_info.purchase.total.currency as currency, 
      rev_info.purchase.total.amount as REV 
      ,rev_info.purchase.virtual_items.items.sku  as sku 
    FROM `gcs.rev` 
    ) 
WHERE currency = 'USD' 

錯誤

"Error: Cannot access field sku on a value with type ARRAY> at [9:59]"

然而

SELECT * 
FROM 
    (
    SELECT 
      rev_info.user.id as player_id, 
      rev_info.purchase.total.currency as currency, 
      rev_info.purchase.total.amount as REV 
      --,rev_info.purchase.virtual_items.items.sku as sku 
    FROM `gcs.rev` 
    ) 
WHERE currency = 'USD' 

該查詢是罰款。

還要注意的是

SELECT 
     rev_info.purchase.virtual_items.items.sku  as sku 
FROM `gcs.rev` 

失敗,同樣的錯誤如上。

+0

你的意思是「無法ŧ o不嵌套...「?你甚至沒有試過!至少這是它在你的問題中看起來的樣子! –

+0

你好,歡迎來到Stackoverflow!如果您收到的答案以任何方式幫助您或解決了您的問題,請考慮接受並投票,因爲這在此論壇中很重要:https://stackoverflow.com/help/someone-answers –

回答

1

擴大對艾略特的答案 - 我覺得這裏首先需要UNNEST,但你很可能需要聚合回你sku的。否則,你會得到相當多餘的(扁平化)輸出

低於我的感覺是,你可能需要的東西 - 它是BigQuery的標準SQL

#standardSQL 
SELECT 
    player_id, 
    currency, 
    REV, 
    STRING_AGG(sku) SKUs 
FROM (
    SELECT 
    rev_info.user.id AS player_id, 
    rev_info.purchase.total.currency AS currency, 
    rev_info.purchase.total.amount AS REV, 
    item.sku AS sku 
    FROM `gcs.rev` t, 
    UNNEST(t.rev_info.purchase.virtual_items.items) item 
) 
WHERE currency = 'USD' 
GROUP BY 1, 2, 3 

因此,所有SKU將作爲一個列表給出player_id,隨着量和貨幣

增加,按照艾略特的意見/建議

#standardSQL 
SELECT 
    rev_info.user.id AS player_id, 
    rev_info.purchase.total.currency AS currency, 
    rev_info.purchase.total.amount AS REV, 
    (SELECT STRING_AGG(item.sku) 
    FROM UNNEST(t.rev_info.purchase.virtual_items.items) item 
) AS SKUs 
FROM `gcs.rev` t, 
WHERE currency = 'USD' 
+0

或者'ARRAY(SELECT sku FROM UNNEST(t.rev_info.purchase.virtual_items.items))'AS sku避免聚集(您可以使用'STRING_AGG'替代)。 –

+0

完全同意。如果它是我的代碼 - 我很可能會使用像'(SELECT STRING_AGG(item.sku)FROM UNNEST(...)item)AS SKUs'沒有'GROUP BY'和沒有'SELECT *'等。我在SO上了解到,在過去兩年的每一天回答是,通常OP在許多情況下試圖「簡化」/混淆他們的代碼,使他們的外部「小」離開他們未來但非常重要的部分,但通常他們不要轉換/更改查詢的結構。所以在這種情況下 - 'SELECT *'看起來有點可疑,所以我試圖不改變內部查詢 –

1

如果您的目標是爲每個items數組元素獲取一行,則可以在表和rev_info.purchase.virtual_items.items之間使用逗號(join)運算符。例如,

SELECT * 
FROM (
    SELECT 
    rev_info.user.id as player_id, 
    rev_info.purchase.total.currency as currency, 
    rev_info.purchase.total.amount as REV, 
    item.sku as sku 
    FROM `gcs.rev` t, 
    t.rev_info.purchase.virtual_items.items item 
) 
WHERE currency = 'USD' 
+0

謝謝,確實有用!然而,我很困惑,爲什麼它是必要的SKU,而不是轉速或貨幣。特別是,我不明白爲什麼標準顯示的自動展平並不直接做到這一點。 – user2998362

+0

只有'sku'是必須的,因爲包含它的字段('items')是一個數組。對於其他字段路徑,例如'currency'和'amount',沿路徑沒有數組。在使用標準SQL時,不存在「自動展平」;你必須明確地表達你的意圖(就像在這種情況下用逗號運算符)。 –