简体   繁体   English

在 Google Bigquery 中访问 ARRAY STRUCT 中的值

[英]accessing values in an ARRAY STRUCT in Google Bigquery

I am new to querying in Google BigQuery and am attempting to flatten an ARRAY field in my query so that the array values are listed as a single result in a comma separated list.我不熟悉在 Google BigQuery 中进行查询,并试图在我的查询中展平 ARRAY 字段,以便将数组值列为逗号分隔列表中的单个结果。 In my query "associations.associatedvids" is an array field in the deals table.在我的查询中,“associations.associatedvids”是交易表中的一个数组字段。 My issue is really is a 2 step problem as I also need to match the associatedvids with the corresponding first and last name fields in another table called contacts.我的问题实际上是一个两步问题,因为我还需要将关联的视频与另一个名为联系人的表中相应的名字和姓氏字段相匹配。 First, for the contact ids, when I do the following首先,对于联系人 ID,当我执行以下操作时

Select
CAST(property_hs_object_id.value AS String) AS deal_ID,
associations.associatedvids AS associated_contacts_ID
From hubspot_data.deals

I get a result like this:我得到这样的结果:

Row    deal_ID         associated_contacts_ID.value 
1      1814103617      3240001
                       3239951
...

but what I want is:但我想要的是:

Row    deal_ID         associated_contacts_ID.value 
1      1814103617      3240001,3239951
...

I've tried different ways of unnesting the array, but cannot seem to get it right.我尝试了不同的方法来取消嵌套数组,但似乎无法正确处理。 For instance the following attempt returns the error "Scalar subquery produced more than one element".例如,以下尝试返回错误“标量子查询产生了多个元素”。

Select
CAST(property_hs_object_id.value AS String) AS deal_ID,
(select associations.associatedvids from unnest(associations.associatedvids)) AS associated_contacts_ID
From hubspot_data.deals

Second, what I ultimately want is:其次,我最终想要的是:

Row    deal_ID         associated_contact_names 
1      1814103617      John Doe,Jane Doe
...

The names fields are property_firstname.value and property_lastname.value, and associations.associatedvids (data type ARRAY<STRUCT>)=contacts.vids (data type INT64).名称字段是 property_firstname.value 和 property_lastname.value,以及 associations.associatedvids(数据类型 ARRAY<STRUCT>)=contacts.vids(数据类型 INT64)。 I've tired the following, but since the data types are different I'm getting an error.我厌倦了以下内容,但由于数据类型不同,我收到了一个错误。

Select
CAST(property_hs_object_id.value AS String) AS deal_ID,
(select concat(property_firstname.value, " ", property_lastname.value)
 from hubspot_data.contacts
 where contacts.vid=associations.associatedvids) AS contact_name
From hubspot_data.deals

Any guidance would be much appreciated!任何指导将不胜感激!

EDIT: Here's is my attempt at a minimal working example piece of code.编辑:这是我对最小工作示例代码的尝试。 I believe the field I'm trying to query is an ARRAY of STURCTs with the data type of the Struct element I want being INT64.我相信我要查询的字段是一个 STURCT 数组,其中 Struct 元素的数据类型是我想要的 INT64。

WITH deals AS (
  Select "012345" as deal_ID,
    [STRUCT(["abc"] as company_ID, [123,678,810] as contact_ID)]
      AS associations)
SELECT 
  deal_ID,
  contacts
FROM deals d
CROSS JOIN UNNEST(d.associations) as contacts

this give me:这给我:

Row    deal_ID    contacts.company_ID    contacts.contact_ID    
1      012345     abc                    123
                                         678
                                         810

but what I want is但我想要的是

Row    deal_ID    contacts.contact_ID   
1      012345     123, 678, 810

And ultimately, I need to replace the contact_IDs with the contact first and last names that are in a different table (but fortunately not in an array).最后,我需要用不同表中的联系人名字和姓氏替换 contact_ID(幸运的是不在数组中)。

Below is for BigQuery Standard SQL以下是 BigQuery 标准 SQL

Based on limited info in your question - I guess you are missing STRING_AGG in the second query you presented in your question根据您问题中的有限信息 - 我猜您在问题中提出的第二个查询中缺少 STRING_AGG

It should be它应该是

SELECT
  CAST(property_hs_object_id.value AS String) AS deal_ID,
  (SELECT STRING_AGG(associations.associatedvids) FROM UNNEST(associations.associatedvids)) AS associated_contacts_ID
FROM hubspot_data.deals   

Update: answer on updated question更新:回答更新的问题

#standardSQL
SELECT 
  deal_ID,
  ARRAY(
    SELECT AS STRUCT 
      company_ID, 
      ( SELECT STRING_AGG(CAST(id AS STRING), ', ') 
        FROM t.contact_ID id
      ) AS contact_ID 
    FROM d.associations t
  ) AS contacts
FROM deals d

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

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