简体   繁体   中英

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

I am new in using BigQuery.

I have two tables FBA Master and a Country Table. 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.

I am looking to join the FBA master table with the country table by Account id,Ad id and Campaign Id. Hence, importing the country data into the FBA master table and maintaining the same level of aggregation of the FBA master table.

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

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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