Aggregate Functions
Aggregates
- Avg: average on a subquery
- Count: count of all objects in subquery
- Max: maximum on a subquery
- Min: minimum on a subquery
- Sum: sum of all objects in subquery
Aggregate function parameters
subquery: String
filter: String
having: String
ordering: [String]
limit: Int
offset: Int
distincton: [String]
Order of Operations
The process follows a specific order of operations: first, the subquery runs with the filter
applied. If distincton
is set, duplicate rows are removed from the subquery based on the specified columns. Next, aggregation occurs, and the having
filter is applied. Finally, ordering
, limit
and offset
are applied to the results.
subquery: String
Aggregate queries apply the aggregate function to the first field of the provided GraphQL query in the 'subquery' variable. Any additional fields will be used to group the aggregate, as in SQL's 'group by' clause. If you use a sub-selection for additional fields, be aware that the result's field names will be named like this: [OBJECT_NAME]_[FIELD_NAME]. A simple example of setting a subquery for an avg aggregate that will retrieve the average budget of all the Star Wars movies in our database.
GraphQL | JSON Response |
---|---|
|
|
A subquery must be included with all aggregate function parameters queries.
The rest of the documentation will use JSON variables in place of inline variables.
filter: String
The filter
aggregate function parameter allows you to apply criteria to limit the objects included in an operation by filtering the subquery. By specifying conditions, you can refine the dataset to include only those objects that meet certain requirements, effectively narrowing down large datasets to focus on specific subsets of interest. For example, you can use the filter
to find the average budget of films directed by George Lucas.
GraphQL | JSON Response |
---|---|
variables
|
|
having: String
The having
aggregate function is used to filter the results of an aggregate query. It allows you to apply conditions to the aggregated data, refining the dataset based on the outcomes of the aggregation. This means you can focus on specific subsets of interest that meet certain criteria, effectively narrowing down the aggregated results from the subquery.
GraphQL | JSON Response |
---|---|
variables
|
|
ordering: [String]
The ordering
function parameters either orders the objects in descending (desc) or in ascending (asc) order. The following example will retrieve the count of character id that have that planet as their home planet then display the count in descending order.
GraphQL | JSON Response |
---|---|
variables
|
|
limit: Int
The number of responses retrieved from the database can be restricted with limit
. The example below limits the number of responses to two, with no specific order applied.
GraphQL | JSON Response |
---|---|
variables
|
|
offset: Int
The offset
function is used to specify the starting point for retrieving rows and is commonly used in conjunction with limit to implement pagination. The example below counts the number of characters that originate from a specific planet, starting at row 3, and orders the results in descending order.
GraphQL | JSON Response |
---|---|
variables
|
|
distincton: [String]
The distincton
function parameter is used to eliminate duplicate values from the result set, ensuring that only unique entries are returned for a specific column. This is particularly useful when you want to retrieve unique values from a column that might have the same value in different rows, thereby reducing redundancy in your query results. The example below is retrieving the average budget for each director in the Star Wars franchise.
GraphQL | JSON Response |
---|---|
variables
|
|