2016-03-22 56 views
1

表1BIGQUERY合併加入時密鑰爲空

+---------+-----------+--------+ 
| user_id | email  | action | 
+---------+-----------+--------+ 
| 1  | [email protected] | open | 
+---------+-----------+--------+ 
| 2  | null  | click | 
+---------+-----------+--------+ 
| 3  | [email protected] | click | 
+---------+-----------+--------+ 
| 4  | [email protected] | open | 
+---------+-----------+--------+ 

表2

+---------+-----------+--------+ 
| user_id | email  | event | 
+---------+-----------+--------+ 
| 1  | [email protected] | sent | 
+---------+-----------+--------+ 
| null | [email protected] | none | 
+---------+-----------+--------+ 
| 2  | [email protected] | sent | 
+---------+-----------+--------+ 
| 4  | [email protected] | sent | 
+---------+-----------+--------+ 

我想根據加入t1.user_id = t2.user_id,但是當有一個空的鍵,加入上t1.email = t2.email

我嘗試多種方式在BigQuery中加入如:
1)ON COALESCE(t1.user_id,t1.email)= COALESCE(t2.user_id,T2。電子郵件)
2.)ON在t2.user_id不爲空的情況下,則t1.user_id = t2.user_id else t1.email = t2.email end

兩者都不工作。如何才能做到這一點?

+0

SQL具有「顯「和」不是DISTINCT「,但我在BigQuery中看不到。 –

回答

1

我會分流,加入到兩個獨立的:
首先 - 由USER_ID加入

SELECT * 
FROM table1 AS t1 
JOIN table2 AS t2 
ON t1.user_id = t2.user_id 

二 - 通過電子郵件參加了在懷念那些IDS首次加入

SELECT * 
FROM (
    SELECT * FROM table1 
    WHERE user_id NOT IN (
    SELECT t1.user_id 
    FROM table1 AS t1 
    JOIN table2 AS t2 
    ON t1.user_id = t2.user_id 
) 
) t1 
JOIN (
    SELECT * FROM table2 
    WHERE user_id NOT IN (
    SELECT t1.user_id 
    FROM table1 AS t1 
    JOIN table2 AS t2 
    ON t1.user_id = t2.user_id 
) 
) t2 
ON t1.email = t2.email 
+0

謝謝。我真的希望它使用COALESCE。它可能基於鏈接。但是,我猜不支持BigQuery? http://stackoverflow.com/questions/5304184/proper-way-to-refer-to-coalesced-value-in-select-statement – Blake

+0

但它不是:o( –

+0

每https://cloud.google.com/bigquery/query-reference#query-grammar:join_predicate: field_from_one_side_of_the_join = field_from_the_other_side_of_the_join [AND ...] –