【问题标题】:transpose bigquery records and concatenate the result as a string and and alternative array转置 bigquery 记录并将结果连接为字符串和替代数组
【发布时间】:2022-01-10 16:47:31
【问题描述】:

我有下表。

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 字符串, 分隔符字符串)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']

【问题讨论】:

  • 你必须用更离散的术语来解释你的问题。您的输入表是什么样的,您期望的确切输出是什么?这将使人们更容易阅读和回答。
  • 谢谢,我会努力的

标签: sql arrays google-bigquery transpose record


【解决方案1】:

好像你想要这样的东西

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

【讨论】:

猜你喜欢
  • 2022-01-10
  • 1970-01-01
  • 2021-06-02
  • 1970-01-01
  • 2013-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-13
相关资源
最近更新 更多