2017-03-24 102 views
2

舉例來說,如果我有這樣的一個架構包含一個值:檢查,如果一個記錄字段使用標準的SQL(谷歌的BigQuery)

[{ 
    "name": "fullName", 
    "type": "string", 
    "mode": "required" 
}, 
{ 
    "name": "children", 
    "type": "record", 
    "mode": "repeated", 
    "fields": [ 
    { 
     "name": "name", 
     "type": "string", 
     "mode": "nullable" 
    }, 
    { 
     "name": "gender", 
     "type": "string", 
     "mode": "nullable" 
    }, 
    { 
     "name": "age", 
     "type": "integer", 
     "mode": "nullable" 
    }] 
}] 

,當一個人可以有多個孩子和孩子都存儲在重複記錄並可以通過children.name,children.gender和children.age訪問。 如果此人有多個孩子,則此人可能會顯示多行,但所有這些行共享一個行號。

我想選擇從該表在孩子的一排名的一個特定值相匹配的所有行,我想喜歡children.name包含它可能是。

回答

2

我把你的問題看成是 - 如何選擇所有有特定名字的孩子的父母?

#standardSQL 
SELECT * 
FROM yourTable 
WHERE 'child1' IN (SELECT name FROM UNNEST(children)) 

你可以用假數據測試上面如下

#standardSQL 
WITH yourTable AS (
    SELECT 
    'parentA' AS fullName, 
    [STRUCT<name STRING, gender STRING, age INT64> 
     ('child1', 'm', 10), ('child2', 'f', 11), ('child3', 'f', 8) 
    ] AS children UNION ALL 
    SELECT 'parentB', 
    [STRUCT<name STRING, gender STRING, age INT64> 
     ('child4', 'm', 10), ('child5', 'f', 11), ('child6', 'f', 8)] UNION ALL 
    SELECT 'parentC', 
    [STRUCT<name STRING, gender STRING, age INT64> 
     ('child7', 'm', 10), ('child1', 'f', 11), ('child8', 'f', 8)] 
) 
SELECT * 
FROM yourTable 
WHERE 'child1' IN (SELECT name FROM UNNEST(children)) 
+0

沒錯,這就是我想要的。謝謝! – Isa

相關問題