google-bigquery - 正则表达式-如何在 BigQuery 中匹配单词 apple 而不是 pine_apple
问题描述
有两个篮子。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
解决方案
这是不使用正则表达式的替代版本。它依赖于ARRAY_AGG
一个条件来评估NULL
水果是否不是苹果,然后跳过将这些字符串添加到数组中:
#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) AS fruits_in_each_basket,
ARRAY_AGG(IF(LOWER(fruit) = 'apple', fruit, NULL) IGNORE NULLS) AS apple
FROM table1
GROUP BY basket
推荐阅读
- sharepoint - SharePoint 365 - 使用 InfoPath 文件中的动态审批者名称创建工作流
- spring - CAS [v5.3.6] / 设置适当语言的问题
- assembly - 如何从命令行(assembly nasm ubuntu 32bit)获取参数?
- ios - ios swift为pdf文档添加分页
- excel - 如何复制最后一列范围并过去到excel表中的下一列范围(动态)
- sql - 如何在postgresql的另一个表中提取非唯一列
- php - 如何自动将用户名插入登录页面的文本框中以及登录时重定向到使用 CodeIgniter 存储在数据库中的国家/州/城市
- jsf - 添加新代码后未调用“commandButton”操作。它正在再次加载页面但未调用该方法
- angularjs - AngularJS 组件的等价物需要 Angular 组件中的属性
- javascript - 使用javascript将一维数组推到第一个位置的二维数组