Following up on this- Bigquery multiple unnest in a single select
We are using bigquery as our warehousing solution and are trying to push the limit by trying to consolidate. A simple example is client tracking. Client generates revenue, has several touch points on our site, and independently maintains several accounts with us. For a business user wanting to do behavior analysis on clients, they want to track visits, revenue generated and how their accounts impacT retention, we are trying to evaluate if a nested structure would work for us
Below is an example. I have 3 tables.
Clients (C)
C_Key| C_Name
-----|------
1 | ABC
2 | DEF
Accounts (A)
A_Key | C_Key
11 | 1
12 | 1
21 | 2
22 | 2
23 | 2
Revenue (R)
R_Key | C_Key | Revenue
-------|---------|----------
11 | 1 | $10
12 | 1 | $20
21 | 2 | $10
I used array_agg to combine these three into a single nested table that looks like below:
{Client,
Accounts:
[{
}],
Revenue:
[{
}]
}
I want to be able to use multiple unnests in a single query like below
Select client, Count Distinct(Accounts) and SUM(Revenue) from <single nested
table>, unnest accounts, unnest revenue
The expected output are 2 rows,
1,2,$30
2,3,$10
However, having multiple unnests in the same query results in a cross join.
The actual output is
1,2,$60
2,3,$30
Select client, Count Distinct(Accounts) and SUM(Revenue) from <single nested table>, unnest accounts, unnest revenue
I get duplicates. I want to build a generic structure to allow adhoc queries. – Subah