Skip to content

Commit

Permalink
Merge pull request #4554 from KavinduZoysa/group-by-bbe-u7
Browse files Browse the repository at this point in the history
Add bbe for group by/collect
  • Loading branch information
gimantha committed Jun 17, 2023
2 parents 7975986 + 18418b2 commit 8097314
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
37 changes: 37 additions & 0 deletions examples/aggregation/aggregation.bal
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);
}
21 changes: 21 additions & 0 deletions examples/aggregation/aggregation.md
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)
2 changes: 2 additions & 0 deletions examples/aggregation/aggregation.metatags
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
4 changes: 4 additions & 0 deletions examples/aggregation/aggregation.out
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

0 comments on commit 8097314

Please sign in to comment.