繁体   English   中英

计算字符串在 BIGQUERY 中的分隔字段中出现的次数

[英]Count the number of times a string appeared in a delimited field In BIGQUERY

我有一个带有分隔符“->”的数据集,例如:

ROW 1- "Q -> Res -> tes -> Res -> twet"
ROW 2- "rw -> gewg -> tes -> Res -> twet"
ROW 3- "Y -> Res -> Res -> Res -> twet"

我只想计算每一行中“Res”的数量

Output 将是:

ROW 1- 2
ROW 2- 1
ROW 3- 3

我尝试编写以下查询,但它们计数不正确或只计数一次:

    countif(distinct(lower(FIELD_NAME) like '%Res%'))

    count(split(regexp_extract(FIELD_NAME, '(.*?)Res'), '->')) 

    (trim(Array_reverse(split(regexp_extract(FIELD_NAME, '(.*?)Res'),   '->')))

    count(regexp_extract(trim(FIELD_NAME), 'Res')) 

    count(regexp_contains(trim(FIELD_NAME), 'Res'))

考虑下面

select id, 
  ( select count(*)
    from unnest(split(text, ' -> ')) word
    where word = 'Res'
  ) cnt
from your_table           

如果应用于您的问题中的示例数据

在此处输入图像描述

output 是

在此处输入图像描述

您可以尝试使用REGEXP_EXTRACT_ALL()考虑以下方法:

select str,
  array_length(REGEXP_EXTRACT_ALL(str, r'\sRes\s')) as Res_count
  from your_table

Output:

在此处输入图像描述

有关使用此 BigQuery 字符串 function 的更多信息,您可以参考文档。

暂无
暂无

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

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