Bigquery:Regexp_Contains(如 !:))
Biq Query: Regexp_Contains (like in !:))
我需要 return true,当一列中的数据包含来自另一 table 列的某些单词并在 Case When 中使用它们时。我该如何修复它(但我不能使用连接或交叉连接,因为我需要左侧的所有数据 table)?
I need return true, when data in one column contain some word from another table column
我发现将第二个 table 概括为正则表达式并使用它通常最简单:
select m.*,
(case when regexp_contains(m.name, d1.pattern) then 'organic'
when regexp_contains(m.name, d2.pattern) then 'social'
end) as source
from main_table m cross join
(select string_agg(name, '|') as pattern
from dictionary_1
) d1 cross join
(select string_agg(name, '|') as pattern
from dictionary_2
) d2;
在字典冗长的情况下 - 正则表达式模式本身可能会变得昂贵,从而影响查询性能。尝试以下方法作为一种选择
select name,
case
when (select logical_or(regexp_contains(t.name, name)) from dictionary_1) then 'organic'
when (select logical_or(regexp_contains(t.name, name)) from dictionary_2) then 'social_media'
else 'other'
end source
from Main_table t
我需要 return true,当一列中的数据包含来自另一 table 列的某些单词并在 Case When 中使用它们时。我该如何修复它(但我不能使用连接或交叉连接,因为我需要左侧的所有数据 table)?
I need return true, when data in one column contain some word from another table column
我发现将第二个 table 概括为正则表达式并使用它通常最简单:
select m.*,
(case when regexp_contains(m.name, d1.pattern) then 'organic'
when regexp_contains(m.name, d2.pattern) then 'social'
end) as source
from main_table m cross join
(select string_agg(name, '|') as pattern
from dictionary_1
) d1 cross join
(select string_agg(name, '|') as pattern
from dictionary_2
) d2;
在字典冗长的情况下 - 正则表达式模式本身可能会变得昂贵,从而影响查询性能。尝试以下方法作为一种选择
select name,
case
when (select logical_or(regexp_contains(t.name, name)) from dictionary_1) then 'organic'
when (select logical_or(regexp_contains(t.name, name)) from dictionary_2) then 'social_media'
else 'other'
end source
from Main_table t