简体   繁体   English

如何查找和提取 substring BIGQUERY

[英]How to fin and extract substring BIGQUERY

A have a string column at BigQuery table for example:例如,在 BigQuery 表中有一个字符串列:

name名称
WW_for_all_feed WW_for_all_feed
EU_param_1_for_all_feed EU_param_1_for_all_feed
AU_for_all_full_settings_18+ AU_for_all_full_settings_18+
WW_for_us_param_5_for_us_feed WW_for_us_param_5_for_us_feed
WW_for_us_param_5_feed WW_for_us_param_5_feed
WW_for_all_25+ WW_for_all_25+

and also have a list of variables, for example:并且还有一个变量列表,例如:

param_1_for_all
param_5_for_us
param_5 
full_settings

And if string at column "name" contains one of this substrings needs to extract it:如果“名称”列中的字符串包含此子字符串之一,则需要将其提取:

name名称 param参数
WW_for_all_feed WW_for_all_feed None没有任何
EU_param_1_for_all_feed EU_param_1_for_all_feed param_1_for_all param_1_for_all
AU_for_all_full_settings_18+ AU_for_all_full_settings_18+ full_settings完整设置
WW_for_us_param_5_for_us_feed WW_for_us_param_5_for_us_feed param_5_for_us param_5_for_us
WW_for_us_param_5_feed WW_for_us_param_5_feed param_5参数_5
WW_for_all_25+ WW_for_all_25+ None没有任何

I want to try regexp and replace, but don't know pattern for find substring我想尝试正则表达式并替换,但不知道查找 substring 的模式

Use below在下面使用

select name, param
from your_table
left join params 
on regexp_contains(name, param)    

if apply to sample data as in your question如果适用于您问题中的示例数据

with your_table as (
  select 'WW_for_all_feed' name union all
  select 'EU_param_1_for_all_feed' union all
  select 'AU_for_all_full_settings_18+' union all
  select 'WW_for_us_param_5_for_us_feed' union all
  select 'WW_for_all_25+' 
), params as (
  select 'param_1_for_all' param union all
  select 'param_5_for_us' union all
  select 'full_settings' 
)    

output is output 是

在此处输入图像描述

but I have an another issue (updated question) If one of params is substring for another?但我有另一个问题(更新的问题)如果其中一个参数是 substring 另一个?

use below then然后在下面使用

select name, string_agg(param order by length(param) desc limit 1) param
from your_table
left join params 
on regexp_contains(name, param)
group by name

if applied to your updated data sample - output is如果应用于更新的数据样本 - output 是

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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