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.
GET type, COUNT(qid) GROUP BY typeThis 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:
GET buildingStorey.attribute.Name, COUNT(qid) WHERE type = IFCDOOR GROUP BY buildingStorey.attribute.NameThe 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.
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:
GET type AS Class, COUNT(qid) AS Objects GROUP BY typeThe 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.
GET qid, attribute.Name, quantity.*.Height WHERE type = IFCWALL ORDER BY quantity.*.Height DESC LIMIT 5ORDER 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#
GET typeHints
- COUNT(qid) counts the objects of a group.
- GROUP BY type makes one group per class.
Show solution
GET type, COUNT(qid) GROUP BY typeGET type AS Class, COUNT(qid) AS Objects GROUP BY typeA 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.
GET type, COUNT(qid) WHERE type = IFCDOOR GROUP BY typeHints
- The key is the storey name: buildingStorey.attribute.Name.
- Use it both in GET and in GROUP BY.
Show solution
GET buildingStorey.attribute.Name, COUNT(qid) WHERE type = IFCDOOR GROUP BY buildingStorey.attribute.NameGET buildingStorey.attribute.Name AS Storey, COUNT(qid) AS Doors WHERE type = IFCDOOR GROUP BY buildingStorey.attribute.NameGET quantity.*.NetVolume WHERE type = IFCWALLHints
- SUM adds up the values; without GROUP BY the result is one row.
- AS names the column.
Show solution
GET SUM(quantity.*.NetVolume) AS TotalVolume WHERE type = IFCWALLGET SUM(quantity.BaseQuantities.NetVolume) AS TotalVolume WHERE type = IFCWALLGET qid, attribute.Name, attribute.LongName WHERE type = IFCSPACEHints
- ORDER BY attribute.Name sorts ascending.
- LIMIT comes after ORDER BY.
Show solution
GET qid, attribute.Name, attribute.LongName WHERE type = IFCSPACE ORDER BY attribute.Name LIMIT 5GET qid, attribute.Name, attribute.LongName WHERE type = IFCSPACE ORDER BY attribute.Name ASC LIMIT 5That 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 BYand compute counts, sums and lists per group. - Name result columns with
AS. - Sort with
ORDER BYand page withLIMITandOFFSET. - Recognise the warning about ungrouped features and fix it.
Next: Units and parameters, where the result learns to speak metres.