I have a table similar to the following:
| Region | Country | Exports |
|---|---|---|
| North America | US | Tech |
| North America | US | Vehicles |
| North America | US | Food |
| North America | Canada | Tech |
| North America | Canada | Food |
| North America | Canada | Tourism |
I am able to get an aggregated output using the following syntax:
select region, struct(country, array_agg(distinct(exports))) from exports_table group by region, country
which looks like:
[
{
"f0_": {
"region": "North America",
"_field_2": {
"country": "US",
"_field_2": [
"Tech",
"Vehicles",
"Food",
]
}
}
},
{
"f0_": {
"region": "North America",
"_field_2": {
"country": "Canada",
"_field_2": [
"Tech",
"Food",
"Tourism",
]
}
}
},
]
How can I translate this structure 1. to use the fields as keys and 2. better aggregation (as below)? (This is something doable on MySQL using JSON_OBJECTAGG and JSON_ARRAYAGG)
[
{
"North America": {
"US": ["Tech","Vehicles","Food"],
"Canada": ["Tech","Vehicles","Food"]
}
}
]
