问题描述
我正在 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 |