简体   繁体   English

如何在 Google BigQuery 上创建嵌套表并保持相同级别的数据聚合

[英]How to create a nested table on Google BigQuery and maintaining the same level of data aggregation

I am new in using BigQuery.我是 BigQuery 的新手。

I have two tables FBA Master and a Country Table.我有两张表 FBA Master 和一张国家表。 Each table shows the same results but at a different aggregation level.每个表显示相同的结果,但聚合级别不同。 The FBA Master table has data aggregated by Platforms and the Country table has data aggregated by countries. FBA Master 表具有按平台汇总的数据,Country 表具有按国家/地区汇总的数据。

I am looking to join the FBA master table with the country table by Account id,Ad id and Campaign Id.我希望通过帐户 ID、广告 ID 和活动 ID 将 FBA 主表与国家/地区表结合起来。 Hence, importing the country data into the FBA master table and maintaining the same level of aggregation of the FBA master table.因此,将国家数据导入 FBA 主表并保持 FBA 主表的相同聚合级别。

Could you please help with this?你能帮忙吗? I was trying to create a nested script on big query by following some tutorials online but with no results.我试图按照一些在线教程在大查询上创建嵌套脚本,但没有结果。

See attached the link to an example of the data set and my objective table.请参阅附加到数据集示例和我的目标表的链接。

https://docs.google.com/spreadsheets/d/1_GNgLN3_AMW0XExZMEWnWEbLhfma5o3uU2cc-G30Y_M/edit?usp=sharing https://docs.google.com/spreadsheets/d/1_GNgLN3_AMW0XExZMEWnWEbLhfma5o3uU2cc-G30Y_M/edit?usp=sharing

Since your joining is based on Account id,Ad id and Campaign Id, you have to ensure there are no duplicates on both the sides (FBA_Master as well as Country_Master)由于您的加入是基于帐户 ID、广告 ID 和活动 ID,因此您必须确保双方没有重复(FBA_Master 以及 Country_Master)

My solution would be this way我的解决方案是这样的

SELECT A.* EXCEPT(PLATFORM), B.* EXCEPT(COUNTRY), STRUCT(PLATFORM,COUNTRY) FROM
(SELECT ACCOUNT_ID, AD_ID, CAMPAIGN_ID , STRING_AGG(DISTINCT PLATFORM ORDER BY PLATFORM) PLATFORM) A
LEFT JOIN
(SELECT ACCOUNT_ID, AD_ID, CAMPAIGN_ID , STRING_AGG(DISTINCT COUNTRY ORDER BY COUNTRY) COUNTRY) B
ON A.ACCOUNT_ID = B.ACCOUNT_ID AND A.AD_ID = B.AD_ID AND A.CAMPAIGN_ID=B.CAMPAIGN_ID

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

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