如何从 REGEXP_EXTRACT_ALL 透视数组构建

问题描述

我正在 BigQuery 表中收集带有查询参数的 url。我想解析这些网址,然后旋转表格。最后输入数据和预期输出

我发现了两个要合并的查询

这个用于旋转我解析的网址:

select id,max(case when test.name='a' then test.score end) as a,max(case when test.name='b' then test.score end) as b,max(case when test.name='c' then test.score end) as c
from 
(
select a.id,t
from `table` as a,unnest(test) as t
)A group by id

然后我有这个查询来解析网址:

WITH examples AS (
  SELECT 1   AS id,'?foo=bar' AS query,'simple'   AS description
  UNION ALL SELECT 2,'?foo=bar&bar=baz','multiple params'
  UNION ALL SELECT 3,'?foo[]=bar&foo[]=baz','arrays'
  UNION ALL SELECT 4,'','no query'
)
SELECT 
  id,query,REGEXP_EXTRACT_ALL(query,r'(?:\?|&)((?:[^=]+)=(?:[^&]*))') as params,r'(?:\?|&)(?:([^=]+)=(?:[^&]*))') as keys,r'(?:\?|&)(?:(?:[^=]+)=([^&]*))') as values,description
FROM examples

我不确定要解释我的问题。但我认为这是因为当我将查询参数拆分为单独的列时它与第一个查询的格式不匹配,我需要在同一列下 merge 键和值,所以我可以取消嵌套他们正确。

输入数据:

| id    | url                   |
|----   |--------------------   |
| 1     | url/?foo=aaa&bar=ccc  |
| 2     | url/?foo=bbb&bar=ccc  |

预期输出

| id    | foo  | bar |
|----   |----  |---- |
| 1     | aaa  | ccc |
| 2     | bbb  | ccc |

我有完全相同数量的参数

解决方法

在下面使用

select id,max(if(split(kv,'=')[offset(0)] = 'foo',split(kv,'=')[offset(1)],null)) as foo,'=')[offset(0)] = 'bar',null)) as bar
from `project.dataset.table` t,unnest(regexp_extract_all(url,r'[?&](\w+=\w+)')) kv
group by id            

如果应用于您问题中的样本数据 - 输出为

enter image description here