Mapper des boucles imbriquées dans une map DSQL
Mappez des éléments à partir d'une entrée contenant plusieurs boucles imbriquées.
Avant de commencer
- Vous avez créé des structures d'entrée et de sortie. Vous pouvez utiliser les échantillons JSON ci-dessous pour créer vos structures.
Pourquoi et quand exécuter cette tâche
{
"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
}
]
}
La structure de sortie ressemble à ceci :
{
"items": [
{
"store_name": "",
"store_location": "",
"department_name": "",
"salesperson_id": "",
"salesperson_name": "",
"item_name": ""
}
]
}
Procédure
Résultats
{
"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"
}
]
}
Vous pouvez également consulter la vue DSQL Script (Script DSQL) pour voir à quoi ressemble le script complet. Vous pouvez voir que certains éléments utilisent des chemins relatifs et d'autres des chemins absolus :
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
}
)
}