正则表达式 - 如何匹配BigQuery中的单词apple not pine_apple

时间:2018-05-04 17:18:57

标签: google-bigquery

有两个篮子。 Basket_1包含苹果,芒果和橙子。 Basket_2包含2个苹果和2个pine_apples。正则表达式模式“apple”匹配单词apple和pine_apple。请澄清。

#standardSQL
with table1 as(
SELECT "basket_1" as basket,"apple" as fruit UNION ALL
SELECT "basket_1","mango" as fruit UNION ALL
SELECT "basket_2","apple" as fruit UNION ALL
SELECT "basket_2","apple" as fruit UNION ALL
SELECT "basket_2","pine_apple" as fruit UNION ALL
SELECT "basket_2","pine_apple" as fruit UNION ALL
SELECT "basket_1","orange" as fruit 
)
SELECT basket,string_agg(fruit)fruits_in_each_basket,regexp_extract_all(string_agg(fruit),r'(?i)apple')apple from table1 group by basket

2 个答案:

答案 0 :(得分:1)

这是一个不使用正则表达式的替代版本。如果水果不是苹果,那么它依赖SELECT DISTINCT a.Name FROM theTable a LEFT JOIN theTable b ON a.Name = b.Name AND b.Product = 'Car' WHERE a.Product != 'Car' AND b.Product IS NULL ; 条件评估为ARRAY_AGG,然后跳过将这些字符串添加到数组中:

NULL

答案 1 :(得分:1)

根据你提问的方式 - 我真的觉得你的真正的桌子每个篮子只有一排有两个字段:篮子和水果如下

WITH `project.dataset.your_table` AS (
  SELECT "basket_1" AS basket, "apple,mango,orange" AS fruits UNION ALL
  SELECT "basket_2","apple,apple,pine_apple,pine_apple" UNION ALL
  SELECT "basket_3","mango, orange" 
)

如果这是正确的 - 下面是如何处理它

#standardSQL
SELECT basket, fruits,
  ARRAY(SELECT fruit FROM UNNEST(SPLIT(fruits)) fruit WHERE LOWER(fruit) = 'apple') apples
FROM `project.dataset.your_table`

您可以使用以上虚拟数据

来测试/播放它
#standardSQL
WITH `project.dataset.your_table` AS (
  SELECT "basket_1" AS basket, "apple,mango,orange" AS fruits UNION ALL
  SELECT "basket_2","apple,apple,pine_apple,pine_apple" UNION ALL
  SELECT "basket_3","mango, orange" 
)
SELECT basket, fruits,
  ARRAY(SELECT fruit FROM UNNEST(SPLIT(fruits)) fruit WHERE LOWER(fruit) = 'apple') apples
FROM `project.dataset.your_table`   

,结果将是

Row basket      fruits                              apples   
1   basket_1    apple,mango,orange                  apple    
2   basket_2    apple,apple,pine_apple,pine_apple   apple    
                                                    apple    
3   basket_3    mango, orange