在 Big Query 中,连接中是否有一种方法可以使用模糊匹配,或者可能使用正则表达式来匹配一系列值?
例如,我有以下查询,其中“持续时间”值可能相差 +/- 30,因此如果 callhistory.duration = 268 那么它将匹配 calltracking.duration = 292,后者落在 238 到 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)
最佳答案
以下简化示例适用于 BigQuery 标准 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,它看起来像您在问题中使用的
关于sql - BigQuery 模糊匹配连接或使用范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48648600/