BigQuery模糊匹配加入或使用范围

时间:2018-02-06 17:22:55

标签: sql google-bigquery

在Big Query中,联接中是否有一种方法可以使用模糊匹配,或者使用正则表达式匹配一系列值?

例如,我有以下查询,其中“duration”值可能相差+/- 30,所以如果callhistory.duration = 268那么它将匹配calltracking.duration = 292,它在指定的238范围内298.

select 
calltracking.date,
calltracking.calling_phone_number,
calltracking.duration,
callhistory.row_date,
callhistory.callid,
callhistory.calling_pty,
callhistory.duration,
calltracking.start_time_utc,
callhistory.segstart_utc


from

(SELECT 
cast(date(start_time_local) as string) as date,
calling_phone_number,
start_time_utc,
duration,
utm_medium,
utm_source
FROM [xxx:calltracking.calls]) calltracking

left join 

(select 
 *
 FROM [xxx:datamart.callhistory]) callhistory

on (callhistory.calling_pty = calltracking.calling_phone_number) and 
(callhistory.row_date = calltracking.date) and (callhistory.duration = 
calltracking.duration)

1 个答案:

答案 0 :(得分:2)

以下简化示例适用于BigQuery Standard SQL

#standardSQL
WITH `xxx.calltracking.calls` AS (
  SELECT 1 id, 292 duration
), `xxx:datamart.callhistory` AS (
  SELECT 2 id, 268 duration 
)
SELECT 
  t.id tid, 
  t.duration tduration,
  h.id hid,
  h.duration hduration
FROM `xxx.calltracking.calls` t
LEFT JOIN `xxx:datamart.callhistory` h
ON t.duration BETWEEN h.duration - 30 AND h.duration + 30  

注意:这不适用于您在问题中使用的BigQuery #legacySQL