My table has a nested object (record) with a few array (repeated) properties.
there are 4 properties that are nested:
BM.HISTORY.AutoBill.BaseRate
BM.HISTORY.AutoBill.Substatus
BM.HISTORY.AutoBill.Services
BM.HISTORY.AutoBill.Conditions
Each property contains data like these
[
{Label: "Completed....", Amount: 34.3, Quantity: 2},
{Label: "Completed....", Amount: 34.3, Quantity: 2}
,...]
I'd like to extract a simple flat csv like this
OrderId StartWindow Type Label Quantity Amount
123 2022-07-17 13:07:00 UTC BaseRate Completed @ 100% 0 82.6
123 2022-07-17 13:07:00 UTC Services service 1 1 16.1323
123 2022-07-17 13:07:00 UTC Services service 2 1 5
123 2022-07-17 13:07:00 UTC Conditions 10% Time Window Premium 0.826 8.26
234 2022-07-17 13:07:00 UTC BaseRate Completed @ 100% 0 3.6
234 2022-07-17 13:07:00 UTC Services service 1 1 16.1323
234 2022-07-17 13:07:00 UTC Services service 2 1 5
234 2022-07-17 13:07:00 UTC Conditions 10% Time Window Premium 0.826 8.26
- Where StartWindow is a simple field of my table.
- Type is a tag I create based on BILL.XXXX from the for properties above and the other 3 columns will be flattened records inside each array.
- they need to stack as rows
This is what I have done but I cannot flatten it all in a simple csv.
SELECT
Id as OrderId, StartWindow, Billing
FROM
`xxx.xxxx.xxDB ADDRESSxxx`,
UNNEST([
(BM.HISTORY.BILL.BaseRate,'BASE RATE'),
(BM.HISTORY.BILL.Substatus,'SUBSTATUS'),
(BM.HISTORY.BILL.Services,'SERVICES'),
(BM.HISTORY.BILL.Conditions,'CONDITIONS')
]
) as Billing
LIMIT 10

