0

我在 bigquery 表中有一个包含很多逗号分隔的 URL 参数的列。

大多数 URL 参数在大括号中包含 ifmobile 和 ifnotmobile 值。

我想提取这些并将它们写在单独的列中。但是值和长度是不确定的,但它们总是在大括号中,并以 ifnotmobile 或 ifmobile 开头。

这里有一些例子:

网址参数

  1. {"track":"{ifnotmobile:-215181}{ifmobile:-1039775}"}
  2. {"track":"{ifmobile:-1173731}{ifnotmobile:-1173730}"}
  3. {"nccoupon":"%2B5PRDKCLV","track":"{ifmobile:-1029110}{ifnotmobile:159860}"}

在这三个示例中,我希望有两个新列,其中包含所有 ifmobile 和所有 ifnotmobile 值。

提取值

ifnotmobile 列

  1. -215181

  2. -1173730

  3. 159860

列 ifmobile

  1. -1039775

  2. -1173731

  3. -1029110

我很高兴能得到任何帮助。我已经用一些 REGEXP_EXTRACT 厌倦了它,但我失败了。

提前非常感谢,菲尔

4

1 回答 1

2

下面是 BigQuery 标准 SQL

#standardSQL
SELECT params, 
  REGEXP_EXTRACT(params, r'{ifnotmobile:(.*?)}') AS ifnotmobile,
  REGEXP_EXTRACT(params, r'{ifmobile:(.*?)}') AS ifmobile
FROM `project.dataset.table`   

您可以使用示例中的示例数据进行测试和使用,如下例所示

#standardSQL
WITH `project.dataset.table` AS (
  SELECT '{"track":"{ifnotmobile:-215181}{ifmobile:-1039775}"}' params UNION ALL
  SELECT '{"track":"{ifmobile:-1173731}{ifnotmobile:-1173730}"}' UNION ALL
  SELECT '{"nccoupon":"%2B5PRDKCLV","track":"{ifmobile:-1029110}{ifnotmobile:159860}"}' 
)
SELECT params, 
  REGEXP_EXTRACT(params, r'{ifnotmobile:(.*?)}') AS ifnotmobile,
  REGEXP_EXTRACT(params, r'{ifmobile:(.*?)}') AS ifmobile
FROM `project.dataset.table`   

结果

Row params                                                                          ifnotmobile ifmobile     
1   {"track":"{ifnotmobile:-215181}{ifmobile:-1039775}"}                            -215181     -1039775     
2   {"track":"{ifmobile:-1173731}{ifnotmobile:-1173730}"}                           -1173730    -1173731     
3   {"nccoupon":"%2B5PRDKCLV","track":"{ifmobile:-1029110}{ifnotmobile:159860}"}    159860      -1029110     

注意:您可以将提取的值转换为您需要的任何类型 - 可能是 INT64

于 2019-08-25T20:18:35.883 回答