2013-11-01 27 views
0

假設我有一個像BigQuery中的文檔中提到的一個架構:BigQuery的SQL如果超過重複記錄

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:約翰,喬希,哈利

citiesLived:紐約,芝加哥,西雅圖

如何迭代citiesLived並使用條件計數。例如,我想要計算有多少用戶名爲fullName = John的用戶都住在城市Lived.place = newyork和citiesLived.place = chicago,但沒有住在citiesLived.place = seattle。

感謝, 約翰

回答

7

可以使用時省略關鍵字。 (這是沒有記錄的,我會提交一個錯誤以確保它有記錄)

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