Aggregate Functions
Aggregate Functions
Not supported in Subgraph Query Service
This feature is only supported in the SubQuery Native query service, not the Subgraph query service.
Group By
SubQuery supports advanced aggregate functions to allow you to perform a calculation on a set of values during your query.
Aggregate functions are usually used with the GroupBy function in your query.
GroupBy allows you to quickly get distinct values in a set from SubQuery in a single query.
Advanced Aggregate Functions
SubQuery provides the following aggregate functions when in unsafe mode:
sum
(applies to number-like fields) - the result of adding all the values togetherdistinctCount
(applies to all fields) - the count of the number of distinct valuesmin
(applies to number-like fields) - the smallest valuemax
(applies to number-like fields) - the greatest valueaverage
(applies to number-like fields) - the average (arithmetic mean) valuestddevSample
(applies to number-like fields) - the sample standard deviation of the valuesstddevPopulation
(applies to number-like fields) - the population standard deviation of the valuesvarianceSample
(applies to number-like fields) - the sample variance of the valuesvariancePopulation
(applies to number-like fields) - the population variance of the values
SubQuery's implementation of aggregate functions is based on pg-aggregates, you can find more information there.
Important
Please note that you must enable the --unsafe
flag on the query service in order to use these functions. Read more.
Also, note that the --unsafe
command will prevent your project from being run in the SubQuery Network, and you must contact support if you want this command to be run with your project in SubQuery's managed service.