0

我正在尝试将 javascript 用户定义函数应用于组。在下面的代码中, -- group by my_groupinsidetuple被注释掉了。我想在每个 inside 上应用temptest函数。如果 group by 被注释掉,代码就会运行。如果我尝试包含 group by,它会产生一个“标量子查询产生了多个元素”。我应该进行哪些更改,以便我可以为每组输出一个数组 ( )?my_grouptest_datamy_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
4

1 回答 1

2

考虑下面(省略功能部分以保持答案足够紧凑......)

 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 my_group, ARRAY_AGG(DISTINCT item) items
  FROM test_data
  group by my_group 
)
select my_group, item
from tuple,
unnest(test(items)) item    

带输出

在此处输入图像描述

于 2022-01-11T19:16:06.717 回答