BigQuery SQL IF重复记录

时间:2013-11-01 15:57:37

标签: sql if-statement google-bigquery

假设我有一个类似于BigQuery文档中提到的模式:

Last modified                 Schema                 Total Rows   Total Bytes   Expiration
 ----------------- ----------------------------------- ------------ ------------- ------------
  27 Sep 10:01:06   |- kind: string                     4            794
                    |- fullName: string (required)
                    |- age: integer
                    |- gender: string
                    +- phoneNumber: record
                    |  |- areaCode: integer
                    |  |- number: integer
                    +- children: record (repeated)
                    |  |- name: string
                    |  |- gender: string
                    |  |- age: integer
                    +- citiesLived: record (repeated)
                    |  |- place: string
                    |  +- yearsLived: integer (repeated)

假设我们有fullNames:John,josh,harry

citiesLived:newyork,芝加哥,西雅图

如何使用条件遍历citiesLived并进行计数。例如,我想计算有多少名使用fullName = John的用户都曾在城市里居住过.place = newyork和citiesLived.place =芝加哥,但还没有住在citiesLived.place = seattle。

谢谢, 约翰

1 个答案:

答案 0 :(得分:7)

您可以使用OMIT IF关键字。 (这是未记录的,我将提交一个错误以确保它被记录)

SELECT COUNT(*) FROM (
  SELECT fullname, 
    IF (citiesLived.place == 'newyork', 1, 0) as ny,
    IF (citiesLived.place == 'chicago', 1, 0) as chi
  FROM (FLATTEN(name_table, citiesLived))
  OMIT RECORD IF citiesLived.place = 'seattle') 
WHERE fullname = 'John' 
  AND ny == 1 
  AND chi == 1