如何查询BiqQuery表

时间:2017-01-26 15:30:17

标签: google-bigquery

我有一张包含2个重复字段的表格。我正在尝试编写一个返回两个字段中匹配结果的查询。在典型的SQL表中,这可以通过使用多个AND语句来实现,但是当我这样做时,我的表不会返回任何结果。该表具有以下模式:

[
            {
                "name": "id",
                "mode": "nullable",
                "type": "integer"
            },
            {
                "name": "record",
                "mode": "repeated",
                "type": "record",
                "fields": [
                    {
                        "name": "type",
                        "type": "string",
                        "mode": "nullable",
                    },
                    {
                        "name": "category",
                        "type": "string",
                        "mode": "nullable"
                    }
                ]
            }

    ]

此声明未给出结果

SELECT id FROM [dataset.table] where ((record.type = 'alpha' AND record.category = 'A') AND (record.type = 'beta' AND record.category = 'B'))

但这样做:

SELECT id FROM [dataset.table] where ((record.type = 'alpha' AND record.category = 'A') OR (record.type = 'beta' AND record.category = 'B'))

如何让它返回符合所有条件的结果。

2 个答案:

答案 0 :(得分:2)

“匹配所有条件”的含义并不完全清楚。您的第一个查询使用以下谓词

((record.type = 'alpha' AND record.category = 'A') AND
 (record.type = 'beta'  AND record.category = 'B'))

相当于

record.type = 'alpha' AND record.type = 'beta' AND
record.category = 'A' AND record.category = 'B'

显然record.type = 'alpha' AND record.type = 'beta总是FALSE

此外,您没有两个重复的字段,而是包含两个字段的单个重复记录。因此,如果您想要在至少有2个重复记录时返回id,一个满足record.type = 'alpha' AND record.category = 'A'而另一个满足record.type = 'beta' AND record.category = 'B'我建议使用标准SQL:

#standardSQL
SELECT id FROM dataset.table t
WHERE
 EXISTS(SELECT 1 FROM t.record r WHERE r.type = 'alpha' and r.category = 'A') AND
 EXISTS(SELECT 1 FROM t.record r WHERE r.type = 'beta' and r.category = 'B') 

答案 1 :(得分:1)

如果您只想输出符合条件的记录的ID,则以下内容将起作用

#standardSQL
SELECT id
FROM yourTable 
WHERE EXISTS (SELECT 1 FROM UNNEST(record) AS r 
    WHERE r IN (STRUCT('alpha','A'), STRUCT('beta','B')))

如果您还希望仅使用那些匹配的条目输出记录本身,请检查以下查询

#standardSQL
SELECT id, 
  ARRAY(
    SELECT AS STRUCT *  FROM UNNEST(record) AS r 
    WHERE r IN (STRUCT('alpha','A'), STRUCT('beta','B'))
  ) AS record
FROM yourTable 
WHERE EXISTS (SELECT 1 FROM UNNEST(record) AS r 
    WHERE r IN (STRUCT('alpha','A'), STRUCT('beta','B')))

#standardSQL
SELECT * FROM (
  SELECT id, 
    ARRAY(
      SELECT AS STRUCT *  FROM UNNEST(record) AS r 
      WHERE r IN (STRUCT('alpha','A'), STRUCT('beta','B'))
    ) AS record
  FROM yourTable 
)
WHERE ARRAY_LENGTH(record) > 0

如果出于某种原因,您只能使用旧版SQL - 请参阅下文,但请考虑{Big}小组推荐的migrating your stuff to Standard SQL

#legacySQL
SELECT id FROM (
  SELECT id,
    SUM((record.type = 'alpha' AND record.category = 'A') 
      OR (record.type = 'beta' AND record.category = 'B')
    ) WITHIN RECORD AS flag
  FROM yourTable
)
WHERE flag > 0

查看有关聚合函数的WITHIN修饰符的更多信息