查询嵌套可重复记录

时间:2016-10-19 11:08:51

标签: google-bigquery

如果我有一个架构,其中一个表具有可重复的记录' age',并且该可重复记录的结构是:

{年龄:28}

是否可以发出查询来搜索行' age'记录包含"年龄= 28岁和年龄= 56"。

所以在这个数据中:

[id = 1,年龄:[{年龄:28},{年龄:56},{年龄:62}]] [id = 2,年龄:[{年龄:28}]] [id = 3,年龄:[{年龄:28},{年龄:56}]]

只有第1和第3行回来了?

提前致谢。

1 个答案:

答案 0 :(得分:2)

假设记录中没有重复年龄
和YourTable看起来如下

id  age  
1    28  
     56  
     62  
2    28  
3    28  
     56  

for BigQuery Legacy SQL

SELECT *
FROM YourTable
OMIT RECORD IF SUM(age=28 OR age=56) <> 2  

SELECT *, 
  SUM(age=28 OR age=56) WITHIN RECORD AS qualify 
FROM YourTable
HAVING qualify = 2  

for BigQuery Standard SQL(请参阅Enabling Standard SQL

SELECT *
FROM YourTable
WHERE (SELECT COUNT(1) 
        FROM UNNEST(age) AS a 
        WHERE a  = 28 OR a = 56
       ) = 2