我正在尝试将 javascript 用户定义函数应用于组。在下面的代码中, -- group by my_group
insidetuple
被注释掉了。我想在每个 inside 上应用temptest
函数。如果 group by 被注释掉,代码就会运行。如果我尝试包含 group by,它会产生一个“标量子查询产生了多个元素”。我应该进行哪些更改,以便我可以为每组输出一个数组 ( )?my_group
test_data
my_group
#standardSQL create function
CREATE TEMP FUNCTION test(a ARRAY<STRING>)
RETURNS ARRAY< STRING >
LANGUAGE js AS '''
var combine = function(a) {
var fn = function(n, src, got, all) {
if (n == 0) {
if (got.length > 0) {
all[all.length] = got;
} return;
}
for (var j = 0; j < src.length; j++) {
fn(n - 1, src.slice(j + 1), got.concat([src[j]]), all);
} return;
}
var all = [];
for (var i = 1; i < a.length; i++) {
fn(i, a, [], all);
}
all.push(a);
return all;
}
return combine(a)
''';
WITH test_data AS (
SELECT 'Shirt' item, 'Cashless' my_group UNION ALL
SELECT 'Jeans', 'Cashless' UNION ALL
SELECT 'Jeans', 'Cash' UNION ALL
SELECT 'Cap', 'Cash' UNION ALL
SELECT 'Shirt', 'Cash' UNION ALL
SELECT 'Cap', 'Cashless'
),
tuple as (
SELECT ARRAY_AGG(DISTINCT item) items
FROM test_data
--group by my_group (uncommenting it creates error)
)
select * from unnest(test((select items from tuple)))
我正在寻找如下输出:
my_group Item
Cash Shirt
Cash Jeans
Cash Cap
Cash Shirt,Jeans
Cash Shirt,Cap
Cash Jeans,Cap
Cash Shirt,Jeans,Cap
Cashless Shirt
Cashless Jeans
Cashless Cap
Cashless Shirt,Jeans
Cashless Shirt,Cap
Cashless Jeans,Cap
Cashless Shirt,Jeans,Cap