2016-10-06 94 views
0

我想加入兩個表格「表格1」和「表格2」。對於Table1中的每個Id,我必須獲取StartTime和StopTime,並引用DateTime位於Table 1的StartTime和StopTime之間的Table2,然後從表2中檢索相應的Point 1和Point 2.我嘗試將兩個表在on子句中,我檢查了Table1.StartTime> = Table2.DateTime和Table1.StopTime < = Table2.DateTime。但谷歌大查詢不支持on =子句。只是想知道這是否有任何補充。任何幫助,將不勝感激。加入表格時的條款條件

表1:

Id StartTime     StopTime  

1 2016-10-05 12:44:21 UTC 2016-10-05 12:59:31 UTC 

表2:

Id DateTime     Point1  Point2 

1 2016-10-05 12:44:21 UTC  52.9  53.2 
2 2016-10-05 12:44:24 UTC  33.1  90.1 
3 2016-10-05 12:50:47 UTC  52.3  90.8 
4 2016-10-05 12:53:23 UTC  52.3  -111.11 

合力表:

Id StartTime     StopTime    Point1  Point2 

1 2016-10-05 12:44:21 UTC 2016-10-05 12:59:31 UTC 52.9  53.2 
2 2016-10-05 12:44:21 UTC 2016-10-05 12:59:31 UTC 33.1  90.1 
3 2016-10-05 12:44:21 UTC 2016-10-05 12:59:31 UTC 52.3  90.8 
4 2016-10-05 12:44:21 UTC 2016-10-05 12:59:31 UTC 52.3  -111.11 
+0

不BIGQUERY對其中條件相同的限制?如果不是,您可以將其作爲交叉加入+條件(可能會影響性能) – Insac

+0

限制在BigQuery傳統SQL中。如果您使用標準SQL,那麼在連接條件中有更多的選項是有效的。 –

回答

2

對於BigQuery的標準SQL(見Enabling Standard SQL

SELECT Table1.Id, StartTime, StopTime, Point1, Point2 
FROM Table1 join Table2 
ON Table1.StartTime <= Table2.DateTime 
AND Table1.StopTime >= Table2.DateTime 

,或者你可以在ON子句下面使用

ON Table2.DateTime BETWEEN Table1.StartTime AND Table1.StopTime