With Transform with SQL, accessing nested fields in hierarchical data is simple and intuitive.
Fields in nested records can be accessed using the dot syntax. If a field is in an array, we use square braces [] to denote that.
Let’s have a look at the following query:
SELECT customer_id, products[].name product_name, products[].quantity * products[].unit_price total_cost FROM Purchases GROUP BY customer_id, products[].name
The result would be be:
customer_id
product_name
total_cost
1
"Orange"
0.25
1
"Banana"
0.4
1
"Apple"
0.5
Calculations on hierarchical data:
When doing calculations on hierarchical data, the result is placed back in the nested hierarchy. This "target location" affects how an operation works when dealing with arrays.
Example 1
The following query:
SET products[].total_cost = products[].quantity * products[].unit_price;SET number_of_purchased_products = SUM_VALUES(products[].quantity);
Note:total_cost resulted in an array but number_of_purchased_products didn't. This is because some operations like SUM_VALUE return a single value, regardless of how many inputs they have.
Inline operations use the deepest possible location in the nesting as their target location.