简体   繁体   English

如何在 Bigquery 中使用数组/结构/json 将五列混合为一列

[英]How to mix five columns into one with an array/struct/json in Bigquery

I have a table where I have a primary key (cust_id) in one column and five other columns with phone numbers, where I might have repeated or null values.我有一张表,其中一列有一个主键 (cust_id),其他五列有电话号码,我可能在其中重复了 null 值。

在此处输入图像描述

I would need to have all phones in a single column and in a single row for each customer.我需要将每个客户的所有电话都放在一列和一行中。 I am not sure what format is best for this.我不确定哪种格式最适合这个。 I was thinking about an ARRAY or JSON. What would you do?我在考虑一个 ARRAY 或 JSON。你会怎么做? I would like to have something like this:我想要这样的东西:

在此处输入图像描述

I need to have all phones, with no null nor duplicates in a string in a way I can access the phone easly.我需要拥有所有电话,没有 null,也没有重复的字符串,我可以轻松访问电话。 I think having them in a JSON string would be nice to use JSON_EXTRACT_SCALAR.我认为将它们放在 JSON 字符串中使用 JSON_EXTRACT_SCALAR 会很好。

I have thied something like this:我有这样的事情:

WITH phones as (
SELECT '6446177' as id, NULL as phone1,NULL as phone2,11572946 as phone3,NULL as phone4,11572946 as phone5 
UNION ALL
SELECT '8523122' as id, NULL as phone1,NULL as phone2,29703165 as phone3,NULL as phone4,29703165 as phone5 
UNION ALL
SELECT '5606494' as id, NULL as phone1,NULL as phone2,51156520 as phone3,NULL as phone4,32247153 as phone5
UNION ALL
SELECT '6560607' as id, 85137 as phone1,NULL as phone2,22185137 as phone3,NULL as phone4,93361637 as phone5),

telefonos_pivot as (
SELECT id,phone1 as phone from phones
UNION ALL
SELECT id,phone2 as phone from phones
UNION ALL
SELECT id,phone3 as phone from phones
UNION ALL
SELECT id,phone4 as phone from phones
UNION ALL
SELECT id,phone5 as phone from phones),

telefonos_pivot_clean as (
SELECT distinct * from telefonos_pivot
WHERE id IS NOT NULL AND phone IS NOT NULL)
SELECT id,array_agg(phone) as phones FROM telefonos_pivot_clean
GROUP BY id

The results are not as I expected:结果并不如我所料:

在此处输入图像描述

Yes, information is avaliable and easy to access, but this format would not fit well in my project.是的,信息可用且易于访问,但这种格式不适合我的项目。 I need all phones in the same string.我需要所有电话都在同一个字符串中。

There is any way to make these columns into a JSON string or something like that?有什么方法可以将这些列变成 JSON 字符串或类似的字符串?

Thank you!谢谢你!

Consider below approach考虑以下方法

select id, 
  array_to_string(array(
    select distinct phone
    from unnest(regexp_extract_all(to_json_string(t), r'"phone\d+":(\d+)')) phone
  ), ', ') phones,
from `project.dataset.phones` t             

if applied to sample phones data in your question - output is如果应用于您问题中的样本phones数据 - output 是

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM