使用Google BigQuery加入查询语法

时间:2016-03-26 08:01:10

标签: google-bigquery

我正在尝试按如下方式运行查询,但BigQuery似乎不支持它。

SELECT lat, long, spot, spotMAC FROM [data.reports] t1
JOIN [data.operators] t2 
ON t1.spot like CONCAT('%',t2.name,'%')

还有另一种形成查询的方法吗?

2 个答案:

答案 0 :(得分:1)

要实现您的需求,您可以尝试以下方式:

SELECT ...
FROM [Data.reports] t1
INNER JOIN [data.operators] t2
ON t1.<some field>=t2.<some field>
WHERE t2.name CONTAINS t1.spot

如果您可以找到一些可以加入的字段(它不需要是精确的连接条件 - 它只需要包含与您应用的t2.name CONTAINS t1.spot匹配的所有记录,这应该可以工作进一步获得您需要的精确连接条件。)

如果找不到这样的JOIN字段,您可以尝试CROSS JOIN(不需要ON子句):

SELECT ...
FROM [data.reports] t1
CROSS JOIN [data.operators] t2
WHERE t2.name CONTAINS t1.spot

然而,CROSS JOIN的查询仅对合理大小的表成功。

答案 1 :(得分:0)

另一种选择:

下面的

应该给你一个想法(如果你愿意,可以给出起点)
这里的想法是使用额外字段来丰富[data.reports]表,而不是可以用作连接标准

SELECT spot, match FROM JS(
(
  SELECT spot, pat
  FROM (
    SELECT spot FROM [data.reports]
  ) AS t1
  CROSS JOIN (
    SELECT GROUP_CONCAT(UNIQUE(name), '|') AS pat FROM [data.operators]
  ) AS t2
) ,
spot, pat,
"[{name: 'spot', type:'string'},
  {name: 'match', type:'string'}]
",
"function(r, emit) {
  var re = new RegExp(r.pat, 'gi');
  var matches = r.spot.match(re);
  if (matches) {
    for (j=0; j < matches.length; ++j) {
      emit({spot: r.spot, match: matches[j]});
    }
  }
}"
)

现在你有匹配字段和斑点,你可以做JOIN ON xx.match = yy.name
目前尚不清楚外部选择中的哪个字段来自哪个表,所以我将它留在这里 希望你能自己完成这个

以下是测试:

SELECT spot, pat, match FROM JS(
(
  SELECT spot, pat
  FROM (
    SELECT spot FROM 
    (SELECT '11aaa22ccc99' AS spot),
    (SELECT '33aaa44' AS spot),
    (SELECT '55bbb66' AS spot),
    (SELECT '77ddd88' AS spot),
  ) AS t1
  CROSS JOIN (
    SELECT GROUP_CONCAT(UNIQUE(name), '|') AS pat FROM 
    (SELECT 'aaa' AS name),
    (SELECT 'bbb' AS name),
    (SELECT 'ccc' AS name),
  ) AS t2
) ,
spot, pat,
"[{name: 'spot', type:'string'},
  {name: 'pat', type:'string'},
  {name: 'match', type:'string'}]
",
"function(r, emit) {
  var re = new RegExp(r.pat, 'gi');
  var matches = r.spot.match(re);
  if (matches) {
    for (j=0; j < matches.length; ++j) {
      emit({spot: r.spot, pat: r.pat, match: matches[j]});
    }
  }
}"
)