diff --git a/examples/aggregation/aggregation.bal b/examples/aggregation/aggregation.bal new file mode 100644 index 0000000000..c7451256f3 --- /dev/null +++ b/examples/aggregation/aggregation.bal @@ -0,0 +1,37 @@ +import ballerina/io; + +public function main() returns error? { + var orders = [ + {orderId: 1, itemName: "A", price: 23.4, quantity: 2}, + {orderId: 1, itemName: "A", price: 20.4, quantity: 1}, + {orderId: 2, itemName: "B", price: 21.5, quantity: 3}, + {orderId: 1, itemName: "B", price: 21.5, quantity: 3} + ]; + + var items = from var {orderId, itemName} in orders + // The `group by` clause create groups for each `orderId`. + // The `itemName` is a non-grouping key and it becomes a sequence variable. + group by orderId + select [itemName]; + + // List of items per `orderId` + io:println(items); + + var quantities = from var {itemName, quantity} in orders + // The `group by` clause create groups for each `itemName`. + // The `quantity` is a non-grouping key and it becomes a sequence variable. + group by itemName + select {itemName, quantity: sum(quantity)}; + + // List of quantity per item + io:println(quantities); + + var income = from var {price, quantity} in orders + let var totPrice = price*quantity + // The `collect` clause creates a single group and all variables become + // non-grouping keys + collect sum(totPrice); + + // Total Income from orders + io:println(income); +} diff --git a/examples/aggregation/aggregation.md b/examples/aggregation/aggregation.md new file mode 100644 index 0000000000..ff5b269936 --- /dev/null +++ b/examples/aggregation/aggregation.md @@ -0,0 +1,21 @@ +# Aggregation + +The `group by` clause in the query expression can group the elements in a collection. Grouping happens based on the grouping keys provided in `group by` clause. For each group, grouping keys are unique. All other variables other than grouping keys are called non-grouping keys. For each group, non-grouping keys become sequence variables. Those variables can be used as a list or an argument to a rest parameter of a langlib function. + +The `collect` clause collects the collection into one group. All the variables become aggregated variables and those variables can be used as a list or an argument to a rest parameter of a langlib function same as in `group by`. + +::: code aggregation.bal ::: + +::: out aggregation.out ::: + +## Related links +- [Query expressions](/learn/by-example/query-expressions) +- [Let clause in query expression](/learn/by-example/let-clause) +- [Limit clause in query expression](/learn/by-example/limit-clause) +- [Joining iterable objects using query](/learn/by-example/joining-iterable-objects) +- [Querying tables](/learn/by-example/querying-tables) +- [Create maps with query expression](/learn/by-example/create-maps-with-query) +- [Create tables with query expression](/learn/by-example/create-tables-with-query) +- [Create streams with query expression](/learn/by-example/create-streams-with-query) +- [On conflict clause in query expression](/learn/by-example/on-conflict-clause) +- [Nested query expressions](/learn/by-example/nested-query-expressions) diff --git a/examples/aggregation/aggregation.metatags b/examples/aggregation/aggregation.metatags new file mode 100644 index 0000000000..e6146e8607 --- /dev/null +++ b/examples/aggregation/aggregation.metatags @@ -0,0 +1,2 @@ +description: This BBE demonstrates how to group a collection, how to handle non grouping keys. +keywords: ballerina, ballerina by example, bbe, group, non-grouping keys, aggregate, collect diff --git a/examples/aggregation/aggregation.out b/examples/aggregation/aggregation.out new file mode 100644 index 0000000000..2636263ed2 --- /dev/null +++ b/examples/aggregation/aggregation.out @@ -0,0 +1,4 @@ +$ bal run aggregation.bal +[["A","A","B"],["B"]] +[{"itemName":"A","quantity":3},{"itemName":"B","quantity":6}] +196.2