Grouping elements
Create a hierarchy from a flat file using the Data Shaping Query Language
GROUP BY and GROUP AS clauses.
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
In this example, you will create a DSQL map to extract data about a store from a flat JSON file and write it to a hierarchical structure.
In the sample JSON below, each object in the items array contains data about an item, including its store and department. In the output, instead of a record per item, you want to first have a record per store, then a record per department in each store, and finally a record per object in each department.
The input data looks like this:
{
"items": [
{
"store_name": "plusstore",
"store_location": "saint louis",
"department_name": "sports",
"salesperson_id": "25",
"item_name": "MBIKE"
},
{
"store_name": "plusstore",
"store_location": "saint louis",
"department_name": "sports",
"salesperson_id": "25",
"item_name": "ESCOOT"
},
{
"store_name": "plusstore",
"store_location": "saint louis",
"department_name": "electronics",
"salesperson_id": "32",
"item_name": "Portable SSD T5"
},
{
"store_name": "plusstore",
"store_location": "saint louis",
"department_name": "electronics",
"salesperson_id": "32",
"item_name": "Extreme Portable SSD"
},
{
"store_name": "goodstore",
"store_location": "nashville",
"department_name": "electronics",
"salesperson_id": "47",
"item_name": "Toshiba P300"
},
{
"store_name": "goodstore",
"store_location": "nashville",
"department_name": "fresh",
"salesperson_id": "47",
"item_name": "Italian tomatoes"
}
]
}
The output structure looks like this:
{
"stores": [
{
"name": "",
"location": "",
"departments": [
{
"name": "",
"salesperson_id": "",
"items": [
{
"name": ""
}
]
}
]
}
]
}
Procedure
Results
{
"stores":[
{
"name":"plusstore",
"location":"saint louis",
"departments":[
{
"name":"electronics",
"salesperson_id":"32",
"items":[
{
"name":"Portable SSD T5"
},
{
"name":"Extreme Portable SSD"
}
]
},
{
"name":"sports",
"salesperson_id":"25",
"items":[
{
"name":"MBIKE"
},
{
"name":"ESCOOT"
}
]
}
]
},
{
"name":"goodstore",
"location":"nashville",
"departments":[
{
"name":"electronics",
"salesperson_id":"47",
"items":[
{
"name":"Toshiba P300"
}
]
},
{
"name":"fresh",
"salesperson_id":"47",
"items":[
{
"name":"Italian tomatoes"
}
]
}
]
}
]
}