Mapping nested loops in a DSQL map
Map elements from an input containing multiple nested loops.
Before you begin
- You have created an input and an output structure. You can use the JSON samples below to create your structures.
About this task
{
"name": "plusstore",
"location": "saint louis",
"departments": [
{
"name": "sports",
"salesperson_id": 25,
"items": [
{
"name": "MBIKE",
"description": "27.5 inch electric mountain bike"
},
{
"name": "ESCOOT",
"description": "Camou electric scooter"
}
]
},
{
"name": "electronics",
"salesperson_id": 32,
"items": [
{
"name": "Portable SSD T5",
"description": "1 TB SSD USB-C"
},
{
"name": "Extreme Portable SSD",
"description": "500 GB SSD USB-C"
}
]
}
],
"salespersons": [
{
"name": "Antonio Guttierez",
"id": 25
},
{
"name": "Gina Monsanto",
"id": 32
}
]
}
The output structure looks like this:
{
"items": [
{
"store_name": "",
"store_location": "",
"department_name": "",
"salesperson_id": "",
"salesperson_name": "",
"item_name": ""
}
]
}
Procedure
Results
{
"items":[
{
"store_name":"plusstore",
"store_location":"saint louis",
"department_name":"sports",
"salesperson_id":"25",
"salesperson_name":"Antonio Guttierez",
"item_name":"MBIKE"
},
{
"store_name":"plusstore",
"store_location":"saint louis",
"department_name":"sports",
"salesperson_id":"25",
"salesperson_name":"Antonio Guttierez",
"item_name":"ESCOOT"
},
{
"store_name":"plusstore",
"store_location":"saint louis",
"department_name":"electronics",
"salesperson_id":"32",
"salesperson_name":"Gina Monsanto",
"item_name":"Portable SSD T5"
},
{
"store_name":"plusstore",
"store_location":"saint louis",
"department_name":"electronics",
"salesperson_id":"32",
"salesperson_name":"Gina Monsanto",
"item_name":"Extreme Portable SSD"
}
]
}
You can also check the DSQL Script view to
see what the full script looks like. You can see that some elements use relative
paths, while others use absolute paths:
FROM plusstore
SELECT {
items = (
FROM plusstore.departments
UNNEST items
JOIN salespersons ON id = salesperson_id
SELECT {
store_name = plusstore.name,
store_location = location,
department_name = plusstore.departments.name,
salesperson_id = salesperson_id,
salesperson_name = name,
item_name = plusstore.departments.items.name
}
)
}