Tutorial·Learn BQL by doing

Grouping and aggregates

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.

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#
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#
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 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#
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 (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#
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 ORDER BY, LIMIT and OFFSET.

Your turn#

GoalDemo model · Schependomlaan
Count the objects of each IFC class in the model.
GET type
Editing and checking need JavaScript; the solution is below, and every query runs in the app's Actions tree too.
Hints
  1. COUNT(qid) counts the objects of a group.
  2. GROUP BY type makes one group per class.
Show solution
GET type, COUNT(qid) GROUP BY type
Also accepted
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.

GoalDemo model · Schependomlaan
Count the doors per storey, showing the storey name.
GET type, COUNT(qid) WHERE type = IFCDOOR GROUP BY type
Editing and checking need JavaScript; the solution is below, and every query runs in the app's Actions tree too.
Hints
  1. The key is the storey name: buildingStorey.attribute.Name.
  2. Use it both in GET and in GROUP BY.
Show solution
GET buildingStorey.attribute.Name, COUNT(qid) WHERE type = IFCDOOR GROUP BY buildingStorey.attribute.Name
Also accepted
GET buildingStorey.attribute.Name AS Storey, COUNT(qid) AS Doors WHERE type = IFCDOOR GROUP BY buildingStorey.attribute.Name
GoalDemo model · Schependomlaan
Compute the total net volume of all walls, in a column named TotalVolume.
GET quantity.*.NetVolume WHERE type = IFCWALL
Editing and checking need JavaScript; the solution is below, and every query runs in the app's Actions tree too.
Hints
  1. SUM adds up the values; without GROUP BY the result is one row.
  2. AS names the column.
Show solution
GET SUM(quantity.*.NetVolume) AS TotalVolume WHERE type = IFCWALL
Also accepted
GET SUM(quantity.BaseQuantities.NetVolume) AS TotalVolume WHERE type = IFCWALL
GoalDemo model · Schependomlaan
List the first five spaces in alphabetical order of their name, with name and LongName.
GET qid, attribute.Name, attribute.LongName WHERE type = IFCSPACE
Editing and checking need JavaScript; the solution is below, and every query runs in the app's Actions tree too.
Hints
  1. ORDER BY attribute.Name sorts ascending.
  2. LIMIT comes after ORDER BY.
Show solution
GET qid, attribute.Name, attribute.LongName WHERE type = IFCSPACE ORDER BY attribute.Name LIMIT 5
Also accepted
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, where the result learns to speak metres.