-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4686 from praneesha/add-aggregation-bbe
Add the aggregation BBE to master branch
- Loading branch information
Showing
4 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
$ bal run aggregation.bal | ||
[["A","A","B"],["B"]] | ||
[{"itemName":"A","quantity":3},{"itemName":"B","quantity":6}] | ||
196.2 |