转置 bigquery 记录并将结果连接为字符串和替代数组

transpose bigquery records and concatenate the result as a string and and alternative array

我有以下 table.

create or replace table t1.cte1 as
WITH t1 AS (
  SELECT 1 as id,'eren' AS last_name UNION ALL
  SELECT 1 as id,'yilmaz' AS last_name UNION ALL
  SELECT 1 as id,'kaya' AS last_name
)
SELECT id,ARRAY_AGG(STRUCT(last_name)) AS last_name_rec
FROM t1
GROUP BY id;

with test as (
select x.id, x.lname_agg,y.last_name  from
(
select id, STRING_AGG(h.last_name,' ') lname_agg FROM
  t1.cte1
  LEFT JOIN
  UNNEST(last_name_rec) AS h
  group by id
  ) x,
  (select id,h.last_name last_name  FROM
  t1.cte1
  LEFT JOIN
  UNNEST(last_name_rec) AS h
  group by last_name,id) y
) select id ,sp.string_flatten_dedup( lname_agg,' ') concat_last_name, last_name from test;

我得到以下输出。我怎样才能使它更有效率。 final output 1

我使用的函数如下

创建或替换函数 sp.string_flatten_dedup(string_value 字符串, delim 字符串) AS (

                        ARRAY_TO_STRING
                            (ARRAY(SELECT distinct string_value
                                   FROM UNNEST(SPLIT(string_value, delim)) AS string_value
                                   order by string_value desc, string_value),
                             delim)

);

我还希望能够将 concat_last_name 中的数据存储为数组 具有如下所示的数据结构。

id, last_name,last_name_array
1, 'eren',[' eren',yilmaz','kaya']
1, 'yilmaz',[' eren',yilmaz','kaya']
1, 'kaya',[' eren',yilmaz','kaya']

看来你想要这样的东西

WITH t1 AS (
  SELECT 1 as id,'eren' AS last_name UNION ALL
  SELECT 1 as id,'yilmaz' AS last_name UNION ALL
  SELECT 1 as id,'kaya' AS last_name
)
SELECT id, last_name, ARRAY_AGG(last_name) OVER (PARTITION BY id) AS last_name_rec
FROM t1

或者如果你想要数组中的字符串值,可以像这样

WITH t1 AS (
  SELECT 1 as id,'eren' AS last_name UNION ALL
  SELECT 1 as id,'yilmaz' AS last_name UNION ALL
  SELECT 1 as id,'kaya' AS last_name
)
SELECT id, last_name, TO_JSON_STRING(ARRAY_AGG(last_name) OVER (PARTITION BY id)) AS last_name_rec
FROM t1