如何以简化格式输出sql数组?

cyvaqqii  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(382)

在bigquery上使用标准sql,我将许多电子邮件列组合成一个独特的电子邮件数组。
但是输出(通过excel上的powerquery查询)显示为

{"v":[{"v":"joe@fakemail.com"},{"v":"joe.bro@someemail.com"}]}

我第一次看到这个。
这看起来像一个键值对象,其中一个数组作为值,然后每个数组值又是一个键值对象。这是正常输出吗(我正在粘贴下面的相关代码段)。
如何输出[joe@fakemail.com ,乔。bro@someemail.com]或[“joe@fakemail.com“乔。bro@someemail.com"].
以下是导致此输出的sql代码:

WITH emails AS (
    select 'Joe' as name, 'joe@hotmail.com,joe@someemail.com' as email1, 'joe@hotmail.com' as email2
    UNION ALL
    select 'Brenda', 'brenda@mail.com', 'Brenda81@TEST.com'
)

select name, 
ARRAY(
    SELECT DISTINCT REPLACE(TRIM(LOWER(EMAIL))," ","") from UNNEST(
        ARRAY_CONCAT(
            SPLIT(email1, ","),
            SPLIT(email2, ",")
        )
    ) AS EMAIL WHERE EMAIL like '%@%'
) AS UNIQUE_EMAILS,  
FROM emails
cwxwcias

cwxwcias1#

改为使用下面的不太详细的版本(也可以使用较少的字符串到数组到字符串的来回-因此应该更好地执行)

select name, 
  (select string_agg(distinct lower(email))
  from unnest(split(concat(email1, ',', email2))) email
  ) unique_emails
from emails

如果应用于问题的样本数据-输出为

igetnqfo

igetnqfo2#

解决方案是使用 ARRAY_TO_STRING() .

WITH emails AS (
    select 'Joe' as name, 'joe@hotmail.com,joe@someemail.com' as email1, 'joe@hotmail.com' as email2
    UNION ALL
    select 'Brenda', 'brenda@mail.com', 'Brenda81@TEST.com'
)

select name,
       ARRAY_TO_STRING(
            ARRAY(
                SELECT DISTINCT REPLACE(TRIM(LOWER(EMAIL))," ","") from UNNEST(
                    ARRAY_CONCAT(
                        SPLIT(email1, ","),
                        SPLIT(email2, ",")
                        )
                    ) AS EMAIL WHERE EMAIL like '%@%'
                ),
           ','
           ) AS UNIQUE_EMAILS,
FROM emails

相关问题