---
title: Grouping and aggregates
summary: Count and sum per group with GROUP BY, name result columns with AS, and order and page the result with ORDER BY, LIMIT and OFFSET.
order: 50
terms: [bql, qid]
see-also: [tutorials/bql/relations, tutorials/bql/units-and-parameters, reference/bql/group-by, reference/bql/order-by]
---

# Grouping and aggregates

So far every query returned one row per object. `GROUP BY` folds the rows that share a value into one, and an aggregate function says what to compute over each group.

```bql {#count-by-type}
GET type, COUNT(qid) GROUP BY type
```

This is the one query to remember: it tells you what a model contains. `COUNT(qid)` counts the objects in the group; the group key (`type`) is repeated in `GET` so you can see which group a row is. Any feature can be a key, including a related one, and several keys make nested groups:

```bql {#doors-per-storey}
GET buildingStorey.attribute.Name, COUNT(qid) WHERE type = IFCDOOR GROUP BY buildingStorey.attribute.Name
```

## The aggregate functions

| Function | Result |
| --- | --- |
| `COUNT(qid)` | number of objects in the group |
| `SUM(x)`, `AVG(x)`, `MIN(x)`, `MAX(x)` | over the numeric values of `x` |
| `MEDIAN(x)`, `MODE(x)`, `RANGE(x)`, `STDDEV(x)` | more statistics over `x` |
| `COLLECT(x)` | the values of `x` as a list |
| `FIRST(qid)` | one representative object of the group |

Numeric aggregates skip values that are not numbers, and a `SUM` over quantities keeps the unit. Without a `GROUP BY` the whole result is one group, so `GET SUM(quantity.*.NetVolume) WHERE type = IFCWALL` is the total wall volume of the model. `COLLECT` is useful with a key: `GET typeObject.attribute.Name, COLLECT(attribute.Name) WHERE type = IFCWALL GROUP BY typeObject.attribute.Name` lists the walls per wall type. All functions are described in [[bql:group-by]].

```warning
A `GET` item that is neither aggregated nor a group key is allowed, but its value comes from an arbitrary member of the group. The editor warns about it; when you see the warning, either add the feature to `GROUP BY` or wrap it in `COLLECT`.
```

## Naming columns

`AS` gives a result column a name, which matters as soon as another program reads the result:

```bql {#alias}
GET type AS Class, COUNT(qid) AS Objects GROUP BY type
```

The alias may be an identifier or a quoted string (`AS "Number of objects"`). In the JSON records it names the aggregate (`Objects` instead of `COUNT`); a group key or a plain feature keeps its path as its key (`type`), and its alias only labels the column of a table export ([[bql:get#alias]]).

## Ordering and paging

`ORDER BY` sorts the rows on one or more features, ascending unless `DESC` is added; `LIMIT` and `OFFSET` then page through them. The clauses come in a fixed order at the end of the statement: `GROUP BY`, `ORDER BY`, `LIMIT`, `OFFSET`.

```bql {#order}
GET qid, attribute.Name, quantity.*.Height WHERE type = IFCWALL ORDER BY quantity.*.Height DESC LIMIT 5
```

`ORDER BY` takes plain features only, never an aggregate: ordering groups by their count is not possible in the language, sort the result in your program instead. See [[bql:order-by]].

## Your turn

```lesson {#lesson-1}
goal: Count the objects of each IFC class in the model.
show: json
check: rows
starter: |
  GET type
hints: |
  - COUNT(qid) counts the objects of a group.
  - GROUP BY type makes one group per class.
solution: |
  GET type, COUNT(qid) GROUP BY type
accept: |
  - GET type AS Class, COUNT(qid) AS Objects GROUP BY type
```

A grouped record has no `qid` — there is no single object behind it — so the round compares the rows themselves. That also means the column names do not matter: the second accepted query names both columns with `AS` and passes just the same.

```lesson {#lesson-2}
goal: Count the doors per storey, showing the storey name.
show: table
check: rows
starter: |
  GET type, COUNT(qid) WHERE type = IFCDOOR GROUP BY type
hints: |
  - The key is the storey name: buildingStorey.attribute.Name.
  - Use it both in GET and in GROUP BY.
solution: |
  GET buildingStorey.attribute.Name, COUNT(qid) WHERE type = IFCDOOR GROUP BY buildingStorey.attribute.Name
accept: |
  - GET buildingStorey.attribute.Name AS Storey, COUNT(qid) AS Doors WHERE type = IFCDOOR GROUP BY buildingStorey.attribute.Name
```

```lesson {#lesson-3}
goal: Compute the total net volume of all walls, in a column named TotalVolume.
show: table
check: rows
require: TotalVolume
require-message: the column has to be named TotalVolume, which is what AS is for
starter: |
  GET quantity.*.NetVolume WHERE type = IFCWALL
hints: |
  - SUM adds up the values; without GROUP BY the result is one row.
  - AS names the column.
solution: |
  GET SUM(quantity.*.NetVolume) AS TotalVolume WHERE type = IFCWALL
accept: |
  - GET SUM(quantity.BaseQuantities.NetVolume) AS TotalVolume WHERE type = IFCWALL
```

```lesson {#lesson-4}
goal: List the first five spaces in alphabetical order of their name, with name and LongName.
show: table
check: sequence
starter: |
  GET qid, attribute.Name, attribute.LongName WHERE type = IFCSPACE
hints: |
  - ORDER BY attribute.Name sorts ascending.
  - LIMIT comes after ORDER BY.
solution: |
  GET qid, attribute.Name, attribute.LongName WHERE type = IFCSPACE ORDER BY attribute.Name LIMIT 5
accept: |
  - GET qid, attribute.Name, attribute.LongName WHERE type = IFCSPACE ORDER BY attribute.Name ASC LIMIT 5
```

That round is the only one so far where the *order* of the rows is part of the answer, and the check says so: the same five spaces in another order comes back as "the right rows in the wrong order".

## You can now

- Turn rows into groups with `GROUP BY` and compute counts, sums and lists per group.
- Name result columns with `AS`.
- Sort with `ORDER BY` and page with `LIMIT` and `OFFSET`.
- Recognise the warning about ungrouped features and fix it.

Next: [Units and parameters](units-and-parameters.md), where the result learns to speak metres.
