AMQL Docs
AQL ReferenceFunctionsTable functions

group

AQL Syntax: Group data into one or multiple dimensions

Definition

Group data by one or multiple dimensions. In AQL, the group function serves as an intermediate step for subsequent transformations. If used by itself, group will return a new table with all unique combination of values in the specified dimensions. To understand more about how group works, please refer to the Concepts page.

Syntax

group(table, dimension, dimension, ...)
Examples
group(products, products.category_id, products.name) // -> Table(products.category_id, products.name)

// with pipe
products | group(products.category_id, products.name) // -> Table(products.category_id, products.name)

Input

  • table: A model reference or the returned table from a previous expression.
  • dimension (repeatable): A fully-qualified reference to a dimension. The output table will have one row for each unique combination of values in the specified dimensions.

Output

A new table with one row for each unique combination of values in the specified dimensions.

Sample Usages

products | group(products.category_id, products.name) 
products->category_idproducts->name
16Coffee Table
15Single Bed
11LG Smart TV
3Samsung Tablet 2
8Chips 1
17Umbrella
11Panasonic Smart TV
10Xbox
2Nokia C2
20Adidas Shoes
22Gucci T-shirt
24Face Cleanser
16Sofa 2
3Samsung Tablet 1
8Chips 2. Dried Fruit
16TV Stand
20Reebox Shoes
22EPTM T-shirt
24Face Masks
22Lacoste T-shirt
18Shower curtains
7Lemon Candy
11Samsung Smart TV
17Outdoor Lighting
4Misfit Vapor
14Dining Table 2
3iPad 1
15Dresser
15Bedroom Mirror
12Samsung Bluetooth Earphone
4Ticwatch 1
15Wardrobe
3Kindle Fire 1
25Body Treatments
24Face Treatment
2Samsung Galaxy 2
17Outdoor Sofa
11Toshiba Smart TV
6Flour 2
6Chocolate Baking
2BlackBerry Key 1
8Cookies 1
24Face Moisturizers
2210 Deep T-shirt
7Chocolate 1
14Dining Chair 1
14Dining Chair 2
18Bathroom Slippers
4Apple Watch 2
10Playstation 3
20Puma Shoes
21Diesel Jeans
4Apple Watch 1
2iPhone X
15Smart Lighting
24Essence Lotions
16Sofa 1
15Bunk Bed
4Ticwatch 2
10Wii
12Sony Headphone 2
24Face Mask
2iPhone 7
16Fireplace
24Face Cream
3iPad 2
25Body Scrub
6Baking Soda 1
17Plant Pot
7Chocolate 2
15Queen Bed
25Body Cleanser
3Kindle Fire 2
24Face Serum
22H&M T-shirt
6Oil 1
21Guess Jeans
18Towels
20Jordan Shoes
21Wrangler Jeans
7KitKat 1
8Cookies 2
20Converse Shoes
22Lucky T-shirt
2Sony Xperia 1
2Nokia C1
12LG Bluetooth Earphone 1
20Nike Shoes
6Flour 1
18Bathroom Mirror
21Calvin Klein Jeans
24Face Oil
18High Cabinet
6Oil 2
10Playstation 4
14Dining Table 1
2Samsung Galaxy 1
11Sony Smart TV
10Xbox 360
12Sony Headphone 1
8AFC Biscuits
Open in playground ↗

The special thing about group is that, it allow you to use Aggregation Functions in the context of a row of the grouped table. For example, if you want to create a table with the Total Product for each Product Category, you can use group and then use the sum function inside select to get the total product for each category:

Total product by product category
products | group(products.category_id) | select(products.category_id, count(products.id)) 
products->category_idcount_products->id
2528
418
2116
1421
314
1721
2223
2018
1013
711
1813
223
1616
1516
619
1211
1120
822
2412
Open in playground ↗

This is especially useful when you want to create a metric with nested aggregation. For example, getting the Average of the above table:

Average total product by product category
products 
| group(products.category_id) 
| select(count_product: count(products.id)) 
| avg(count_product)
avg_count_product
17.6315789473684211
Open in playground ↗

See also

On this page