gpt4 book ai didi

google-bigquery - 如何在 BigQuery 标准 SQL 中获取数组的一部分?

转载 作者:行者123 更新时间:2023-12-03 23:28:43 27 4
gpt4 key购买 nike

在 BigQuery 中,我有一个带有 path 的表像这样的列:

ID .     | Path
---------+----------------------------------------
1 | foo/bar/baz
2 | foo/bar/quux/blat

我希望能够在正斜杠( / )上拆分路径并选择一个或多个路径部分,重新​​加入它们。

在 PostgreSQL 中,这很容易:
select array_to_string((regexp_split_to_array(path, '/'))[1:3], '/')

但是 BigQuery 似乎没有任何类型的范围偏移或数组切片功能。

最佳答案

下面是 BigQuery 标准 SQL

#standardSQL
SELECT id, path,
(
SELECT STRING_AGG(part, '/' ORDER BY index)
FROM UNNEST(SPLIT(path, '/')) part WITH OFFSET index
WHERE index BETWEEN 1 AND 3
) adjusted_path
FROM `project.dataset.table`

您可以使用您的问题中的示例数据进行测试,使用上面的示例数据,如下例所示
#standardSQL
WITH `project.dataset.table` AS (
SELECT 1 id, 'foo/bar/baz/foo1/bar1/baz1/' path UNION ALL
SELECT 2, 'foo/bar/quux/blat/foo2/bar2/quux2/blat2'
)
SELECT id, path,
(
SELECT STRING_AGG(part, '/' ORDER BY index)
FROM UNNEST(SPLIT(path, '/')) part WITH OFFSET index
WHERE index BETWEEN 1 AND 3
) adjusted_path
FROM `project.dataset.table`

结果
Row     id      path                                        adjusted_path    
1 1 foo/bar/baz/foo1/bar1/baz1/ bar/baz/foo1
2 2 foo/bar/quux/blat/foo2/bar2/quux2/blat2 bar/quux/blat

如果由于某种原因你想保持你的查询“内联/类似”你在 PostgreSQL (array_to_string((regexp_split_to_array(path, '/'))[1:3], '/')) - 你可以引入 SQL UDF(让我们将其命名为 ARRAY_SLICE )如下例所示
#standardSQL
CREATE temp FUNCTION ARRAY_SLICE(arr ARRAY<STRING>, start INT64, finish INT64)
RETURNS ARRAY<STRING> AS (
ARRAY(
SELECT part FROM UNNEST(arr) part WITH OFFSET index
WHERE index BETWEEN start AND finish ORDER BY index
)
);
SELECT id, path,
ARRAY_TO_STRING(ARRAY_SLICE(SPLIT(path, '/'), 1, 3), '/') adjusted_path
FROM `project.dataset.table`

显然,如果应用于相同的样本数据 - 你会得到相同的结果
#standardSQL
CREATE temp FUNCTION ARRAY_SLICE(arr ARRAY<STRING>, start INT64, finish INT64)
RETURNS ARRAY<STRING> AS (
ARRAY(
SELECT part FROM UNNEST(arr) part WITH OFFSET index
WHERE index BETWEEN start AND finish ORDER BY index
)
);
WITH `project.dataset.table` AS (
SELECT 1 id, 'foo/bar/baz/foo1/bar1/baz1/' path UNION ALL
SELECT 2, 'foo/bar/quux/blat/foo2/bar2/quux2/blat2'
)
SELECT id, path,
ARRAY_TO_STRING(ARRAY_SLICE(SPLIT(path, '/'), 1, 3), '/') adjusted_path
FROM `project.dataset.table`

Row id path adjusted_path
1 1 foo/bar/baz/foo1/bar1/baz1/ bar/baz/foo1
2 2 foo/bar/quux/blat/foo2/bar2/quux2/blat2 bar/quux/blat

关于google-bigquery - 如何在 BigQuery 标准 SQL 中获取数组的一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54835184/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com