如何使用间隔交叉加入Big Query?

时间:2013-11-22 04:25:33

标签: google-bigquery

如何在Google Big Query中使用间隔加入两个表?

我有两张桌子:

Table CarsGPS:
ID | Car | Latitude | Longitude
1  | 1   | -22.123  | -43.123
2  | 1   | -22.234  | -43.234
3  | 2   | -22.567  | -43.567
4  | 2   | -22.678  | -43.678
...

Table Areas:
ID | LatitudeMin | LatitudeMax | LongitudeMin | LongitudeMax
1  | -22.124     | -22.120     | -43.124      | -43.120
2  | -22.128     | -22.124     | -43.128      | -43.124
...

我想通过Google Big Query交叉加入这些表格来检查每辆车已经过了哪些区域。

在常规SQL服务器中,我会:

SELECT A.ID, C.Car 
FROM Cars C, Areas A
WHERE C.Latitude BETWEEN A.LatitudeMin AND A.LatitudeMax AND
      C.Longitude BETWEEN A.LongitudeMin AND A.LongitudeMax

但Google Big Query只允许我使用连接表之间的完全匹配来进行连接(甚至是JOIN EACH)。而“FROM X,Y”意味着UNION,而不是JOINS。

所以,这不是一个选择:

SELECT A.ID, C.Car 
FROM Cars C
JOIN EACH
  Areas A
  ON C.Latitude BETWEEN A.LatitudeMin AND A.LatitudeMax AND
     C.Longitude BETWEEN A.LongitudeMin AND A.LongitudeMax

然后,我怎样才能运行与它类似的东西来识别每个区域内的哪些车辆?

先谢谢了,

1 个答案:

答案 0 :(得分:2)

BigQuery现在支持CROSS JOIN。您的查询将如下所示:

SELECT A.ID, C.Car 
FROM Cars C 
CROSS JOIN Areas A
WHERE C.Latitude BETWEEN A.LatitudeMin AND A.LatitudeMax AND
  C.Longitude BETWEEN A.LongitudeMin AND A.LongitudeMax