GROUP BY
Grouping and the aggregate functions — COUNT, SUM, MIN, MAX, AVG, MEDIAN, MODE, RANGE, STDDEV, COLLECT and FIRST, single and multi-key groups, BIN buckets, and the grouped result shape.
GROUP BY folds the selected objects into groups with the same value for one or more key features, and the aggregate functions in GET summarise each group. Without GROUP BY, an aggregate summarises the whole result.
Syntax#
GET <key feature>, <aggregate>(<feature>) [, ...] [WHERE ...] GROUP BY <key feature> [, <key feature> ...]The key features are plain features (no aggregates, no aliases, no arithmetic); BIN(...) is allowed. A GET item that is neither a key nor an aggregate returns the value of an arbitrary member of the group, which the validator warns about.
GET type, COUNT(qid) GROUP BY typeGET buildingStorey.attribute.Name, COUNT(qid) WHERE type = IFCDOOR GROUP BY buildingStorey.attribute.NameObjects without a value#
The selected objects without a value for the key form one more group, [none], whatever the key: an attribute, a property, a quantity, a material, a classification, a layer, a zone, a system, a related object or a calculated value. Its record has the aggregates but no key column, so the groups always account for every selected object. An object with several values of the key (two materials, three zones) is in each of their groups.
Without a WHERE, everything the models hold is selected, the building storeys, materials and type objects included, and most of it ends up in [none]. Select the objects the grouping is about, or only those with a value:
GET material.name, COUNT(qid) WHERE type = IFCELEMENT GROUP BY material.nameGET material.name, COUNT(qid) WHERE material.name GROUP BY material.nameAggregates#
| Function | Argument | Result |
|---|---|---|
COUNT(<feature>) |
any feature, usually qid |
Number of objects in the group. COUNT(children) is something else: a per-object child count (get#special). |
SUM(<expression>) |
numeric | Sum; non-numeric values are skipped. |
MIN(<expression>), MAX(<expression>) |
numeric | Smallest, largest value. |
AVG(<expression>) |
numeric | Arithmetic mean. |
MEDIAN(<expression>) |
numeric | Median. |
MODE(<expression>) |
numeric | Most frequent value. |
RANGE(<expression>) |
numeric | Largest minus smallest. |
STDDEV(<expression>) |
numeric | Standard deviation. |
COLLECT(<feature>, ...) |
one or more features | The values of every member as a list, one entry per object. |
COLLECT(DISTINCT <feature>, ...) |
one or more features | Every different value once (for several features: every different combination). Members without a value add nothing. |
FIRST(<feature>) |
qid |
One deterministic sample member per group (lowest version UUID, then lowest oid). Other features are warned about. |
The numeric aggregates take an arithmetic expression (AVG(a / b)), with the unit rules of get#arithmetic. UNIT cannot follow an aggregate; convert the whole result with USING and parameters instead. Aggregates cannot be sorted on (ORDER BY, LIMIT and OFFSET).
GET buildingStorey.attribute.Name AS Storey, SUM(quantity.Qto_WallBaseQuantities.NetSideArea) AS SideArea, COUNT(qid) AS Walls
WHERE type = IFCWALL
GROUP BY buildingStorey.attribute.NameGET property.Pset_WallCommon.IsExternal, COLLECT(qid) WHERE type = IFCWALL GROUP BY property.Pset_WallCommon.IsExternalGET type AS Type, COUNT(qid) AS Count, FIRST(qid) AS Sample GROUP BY typeAggregates without GROUP BY#
An aggregate in a query without GROUP BY treats the whole result as one group and returns a single record.
GET COUNT(qid) AS Doors, AVG(quantity.Qto_DoorBaseQuantities.Width) AS AverageWidth WHERE type = IFCDOORMulti-key groups#
Several key features produce one record per non-empty combination, ordered by the first key, then the second. Objects without a value for a key fall into a synthetic [none] group for that key (group-by#none), so every level fully partitions its parent. The API's groupPath option walks such a result level by level; see output#groups.
GET buildingStorey.attribute.Name, type, COUNT(qid)
WHERE type IN (IFCDOOR, IFCWINDOW)
GROUP BY buildingStorey.attribute.Name, typeBuckets: BIN#
BIN(<numeric feature>, <size>) maps a value to the interval of the given size it falls in; as a key it makes histograms. The size is a number with an optional unit.
GET BIN(aabb.height, 500 mm), COUNT(qid) WHERE type = IFCWALL GROUP BY BIN(aabb.height, 500 mm)Grouping on values with metadata#
Grouping on attribute.Name groups on the attribute's value. The explicit .value field (attribute.Name.value, property.Pset_SpaceCommon.Category.value) spells that out and is what older queries in the UI use; both are accepted.
Result shape#
Each group is a record with the key feature(s) under their path and the aggregates under their keyword (COUNT, MAX, …), whatever their alias; SUM and COLLECT are keyed by their full text (SUM(quantity.*.NetVolume), COLLECT(qid)), and so is a second aggregate of the same kind (output#values, get#alias). An aggregate value is a leaf object like any other ({"name": "COUNT", "type": "int", "value": 58, "unit": "UNITLESS"}). COLLECT yields a list. A result with too many groups is rejected as too large: narrow the WHERE, group on a coarser feature or add LIMIT.
Used in#
- Grouping and aggregates — counts and sums per storey.
- Count results —
COUNT(qid)versus the count operation.