Aggregation
|
This documentation is not current - it is for TypeDB 2.x, which is no longer supported. View the documentation for TypeDB 3.x: |
This page covers an aggregation of TypeQL Get query results.
Syntax
TypeQL supports the following types of aggregation:
-
count— Get the total number of results returned. -
sum— Get the sum of thelongordoublevalues of the specified variable. -
max— Get the maximum value among thelongordoublevalues of the specified variable. -
min— Get the minimum value among thelongordoublevalues of the specified variable. -
mean— Get the average value of thelongordoublevalues of the specified variable. -
median— Get the median value among thelongordoublevalues of the matched variable. -
std— Get the sample standard deviation value among thelongordoublevalues of the matched variable.
They are used at the end of a query.
Behavior
A Get query without aggregation or grouping returns a stream of ConceptMaps to represent every matched solution for its pattern. With aggregation, the query performs calculation on the collection of ConceptMaps and returns a single value. Aggregation can be used alongside grouping and in subqueries, which affects the output.
Usage for counting results
For this example, use a database with the IAM schema and sample data loaded.
For any Get query, to count the number of results returned,
add the count keyword at the end of the query with a semicolon.
match
$o isa object, has path $fp;
get;
count;
Notice, that count keyword uses no variables, as it counts only the number of results, regardless of contents.
Changing the variables in the get clause of a query can lead to changing the number of results,
due to deduplication (the same result can’t be returned twice).
See the next example below.
Usage for numeric values
For this example, use a database with the IAM schema and sample data loaded.
For long or double values represented by a value variable or by a concept variable mapped to an attribute,
we can use aggregation to compute an output based on all returned values.
match
$f isa file, has size-kb $s;
get $f, $s;
sum $s;
|
Omitting the variable |