简体   繁体   中英

BigQuery Fuzzy Match Join Or Using A Range

In Big Query is there a way in a join to use a fuzzy match, or match across a range of values possibly using a regular expression?

For example, I have the following query where the "duration" value may differ by +/- 30 so if callhistory.duration = 268 then it would match to calltracking.duration = 292 which falls within the specified range of 238 to 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)

Below simplified example is for 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  

Note: this will not work with BigQuery #legacySQL which looks like you are using in your question

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM