2016-05-03 69 views
0

好吧,所以我試圖將四個不同的表連接在一起,並且它變得非常困難。我提供的每個表的片段中希望大家可以幫幫忙四表加入BigQuery

表1:數據

+--------+--------+-----------+ 
| charge | amount | date | 
+--------+--------+-----------+ 
| 123 | 10000 | 2/10/2016 | 
| 456 | 10000 | 1/28/2016 | 
| 789 | 10000 | 3/30/2016 | 
+--------+--------+-----------+ 

表2:data_metadata

+--------+------------+------------+ 
    | charge | key  | value | 
    +--------+------------+------------+ 
    | 123 | identifier | trrkfll212 | 
    | 456 | code  | test  | 
    | 789 | ID   | 123xyz  | 
    +--------+------------+------------+ 

表3:買家

+-----+-----------+----------+----------+ 
| id | date | discount | plan | 
+-----+-----------+----------+----------+ 
| ABC | 2/13/2016 | yes  | option a | 
| DEF | 2/1/2016 | yes  | option a | 
| GHI | 1/22/2016 | no  | option a | 
+-----+-----------+----------+----------+ 

表4:buyer_metadata

+--------------+-----------+--------+ 
| id | |key|    | value | 
+--------------+-----------+--------+ 
| ABC   | migration | TRUE | 
| DEF   | emid  | foo | 
| GHI   | ID  | 123xyz | 
+--------------+-----------+--------+ 

好的,所以表格數據和data_metadata顯然是通過charge列連接的。

表格買家和buyer_metadata通過id列連接。

但我想將它們連接在一起。我很確定實現這一目標的方法是通過「值」列中的公共字段(在本例中爲123xyz)將元數據錶鏈接在一起。

任何人都可以幫忙嗎?

+1

你想在表之間用作「鏈接」的所有列在每個表中都是UNIQUE? – Delphine

+0

@Delphine我很困惑你在問什麼?數據和data_metadata中的「收費」列顯然是鏈接在一起的。以及買方和買方_元數據中的ID列。但我需要將它們全部鏈接在一起。 – wizkids121

+0

我明白是的,但我想知道你的表之間是否存在「onToMany」關係。例如,在數據表中,這種可能的值「123」是否存在多次?等你的4張表 – Delphine

回答

1

這可能看起來像這樣的事情,如果所有的「鏈接」欄都是獨一無二的:

SELECT * 
FROM data d 
JOIN data_metadata dm ON d.charge = dm.charge 
JOIN buyer_metada bm ON dm.value = bm.value 
JOIN buyer b ON bm.id = b.id 

如果不是這樣,我認爲你必須使用像GROUP BY條款

1

讓我們把它在兩步,首先爲databuyer創建複合表。數據綜合表:

SELECT data.charge, data.amount, data.date, 
     data_metadata.key, data_metadata.value 
FROM [data] AS data 
JOIN (SELECT charge, key, value FROM [data_metadata]) AS data_metadata 
ON data.charge = data_metadata.charge 

和複合表買家:

SELECT buyer.id, buyer.date, buyer.discount, buyer.plan, 
     buyer_metadata.key, buyer_metadata.value 
FROM [buyer] AS buyer 
JOIN (SELECT key, value FROM [buyer_metadata]) AS buyer_metadata 
ON buyer.id = buyer_metadata.id 

然後讓我們加入這兩個複合表

SELECT composite_data.*, composite_buyer.* 
FROM (
    SELECT data.charge, data.amount, data.date, 
      data_metadata.key, data_metadata.value 
    FROM [data] AS data 
    JOIN (SELECT charge, key, value FROM [data_metadata]) AS data_metadata 
    ON data.charge = data_metadata.charge) AS composite_data 
JOIN (
    SELECT buyer.id, buyer.date, buyer.discount, buyer.plan, 
      buyer_metadata.key, buyer_metadata.value 
    FROM [buyer] AS buyer 
    JOIN (SELECT key, value FROM [buyer_metadata]) AS buyer_metadata 
    ON buyer.id = buyer_metadata.id) AS composite_buyer 

ON composite_data.value = composite_buyer.value 

我沒有測試它,但它可能接近。

僅供參考,以下是BigQuery JOINs上的頁面。你見過this SO