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, ...)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_id | products->name |
|---|---|
| 16 | Coffee Table |
| 15 | Single Bed |
| 11 | LG Smart TV |
| 3 | Samsung Tablet 2 |
| 8 | Chips 1 |
| 17 | Umbrella |
| 11 | Panasonic Smart TV |
| 10 | Xbox |
| 2 | Nokia C2 |
| 20 | Adidas Shoes |
| 22 | Gucci T-shirt |
| 24 | Face Cleanser |
| 16 | Sofa 2 |
| 3 | Samsung Tablet 1 |
| 8 | Chips 2. Dried Fruit |
| 16 | TV Stand |
| 20 | Reebox Shoes |
| 22 | EPTM T-shirt |
| 24 | Face Masks |
| 22 | Lacoste T-shirt |
| 18 | Shower curtains |
| 7 | Lemon Candy |
| 11 | Samsung Smart TV |
| 17 | Outdoor Lighting |
| 4 | Misfit Vapor |
| 14 | Dining Table 2 |
| 3 | iPad 1 |
| 15 | Dresser |
| 15 | Bedroom Mirror |
| 12 | Samsung Bluetooth Earphone |
| 4 | Ticwatch 1 |
| 15 | Wardrobe |
| 3 | Kindle Fire 1 |
| 25 | Body Treatments |
| 24 | Face Treatment |
| 2 | Samsung Galaxy 2 |
| 17 | Outdoor Sofa |
| 11 | Toshiba Smart TV |
| 6 | Flour 2 |
| 6 | Chocolate Baking |
| 2 | BlackBerry Key 1 |
| 8 | Cookies 1 |
| 24 | Face Moisturizers |
| 22 | 10 Deep T-shirt |
| 7 | Chocolate 1 |
| 14 | Dining Chair 1 |
| 14 | Dining Chair 2 |
| 18 | Bathroom Slippers |
| 4 | Apple Watch 2 |
| 10 | Playstation 3 |
| 20 | Puma Shoes |
| 21 | Diesel Jeans |
| 4 | Apple Watch 1 |
| 2 | iPhone X |
| 15 | Smart Lighting |
| 24 | Essence Lotions |
| 16 | Sofa 1 |
| 15 | Bunk Bed |
| 4 | Ticwatch 2 |
| 10 | Wii |
| 12 | Sony Headphone 2 |
| 24 | Face Mask |
| 2 | iPhone 7 |
| 16 | Fireplace |
| 24 | Face Cream |
| 3 | iPad 2 |
| 25 | Body Scrub |
| 6 | Baking Soda 1 |
| 17 | Plant Pot |
| 7 | Chocolate 2 |
| 15 | Queen Bed |
| 25 | Body Cleanser |
| 3 | Kindle Fire 2 |
| 24 | Face Serum |
| 22 | H&M T-shirt |
| 6 | Oil 1 |
| 21 | Guess Jeans |
| 18 | Towels |
| 20 | Jordan Shoes |
| 21 | Wrangler Jeans |
| 7 | KitKat 1 |
| 8 | Cookies 2 |
| 20 | Converse Shoes |
| 22 | Lucky T-shirt |
| 2 | Sony Xperia 1 |
| 2 | Nokia C1 |
| 12 | LG Bluetooth Earphone 1 |
| 20 | Nike Shoes |
| 6 | Flour 1 |
| 18 | Bathroom Mirror |
| 21 | Calvin Klein Jeans |
| 24 | Face Oil |
| 18 | High Cabinet |
| 6 | Oil 2 |
| 10 | Playstation 4 |
| 14 | Dining Table 1 |
| 2 | Samsung Galaxy 1 |
| 11 | Sony Smart TV |
| 10 | Xbox 360 |
| 12 | Sony Headphone 1 |
| 8 | AFC Biscuits |
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:
products | group(products.category_id) | select(products.category_id, count(products.id)) | products->category_id | count_products->id |
|---|---|
| 25 | 28 |
| 4 | 18 |
| 21 | 16 |
| 14 | 21 |
| 3 | 14 |
| 17 | 21 |
| 22 | 23 |
| 20 | 18 |
| 10 | 13 |
| 7 | 11 |
| 18 | 13 |
| 2 | 23 |
| 16 | 16 |
| 15 | 16 |
| 6 | 19 |
| 12 | 11 |
| 11 | 20 |
| 8 | 22 |
| 24 | 12 |
This is especially useful when you want to create a metric with nested aggregation. For example, getting the Average of the above table:
products
| group(products.category_id)
| select(count_product: count(products.id))
| avg(count_product)| avg_count_product |
|---|
| 17.6315789473684211 |