Using Functions - Aggregation
This section assumes you have alredy completed configuring the database from the Configuration Section.
Also, for basic knowledge and understanding, please first look at the Making Queries Section
Introduction
This section covers database functions that can be used to aggregate data in the database. Aggregation functions are used to perform calculations on the data in the database. These functions can be used to calculate the sum, average, minimum, maximum, and count of the data in the database.
Inbuilt Aggregation Functions
Some common aggregation functions have already been implemented in PORM. These functions can be used to perform calculations on the data in the database directly.
count
The count
function is used to count the number of records in the database. This function can be used to count the number of records in the database that meet certain conditions.
You can also provide a column name to count the number of records in the database that have a value in the specified column.
You can also provide conditions to count the number of records in the database that meet certain conditions.
The count
function returns the number of records in the database that meet the specified conditions.
sum
The sum
function is used to calculate the sum of the values in a column in the database. This function can be used to calculate the sum of the values in a column in the database that meet certain conditions.
You can also provide conditions to calculate the sum of the values in a column in the database that meet certain conditions.
The sum
function returns the sum of the values in the column in the database that meet the specified conditions.
All methods that take in a condition can be called after the “where” method. This is because the “where” method is used to build the where clause for the query.
avg
The avg
function is used to calculate the average of the values in a column in the database. This function can be used to calculate the average of the values in a column in the database that meet certain conditions.
You can also provide conditions to calculate the average of the values in a column in the database that meet certain conditions.
The avg
function returns the average of the values in the column in the database that meet the specified conditions.
max
The max
function is used to calculate the maximum value in a column in the database. This function can be used to calculate the maximum value in a column in the database that meet certain conditions.
You can also provide conditions to calculate the maximum value in a column in the database that meet certain conditions.
The max
function returns the maximum value in the column in the database that meet the specified conditions.
min
The min
function is used to calculate the minimum value in a column in the database. This function can be used to calculate the minimum value in a column in the database that meet certain conditions.
You can also provide conditions to calculate the minimum value in a column in the database that meet certain conditions.
The min
function returns the minimum value in the column in the database that meet the specified conditions.
All the above methods query the database and return the result. Therefore, you should always call these methods last.
Using the Agg Builder
We have also put aside a builder class that can be used to build more complex aggregation queries. The Agg builder
comes with a number of methods to cover your aggregation needs.
Initializing the Agg Builder
To initialize the Agg builder
, you can use the builder
method.
You must finally call the build
method to get the actually build your generated aggregates.
Agg builder comes with a number of methods that can be used to build the aggregation query. They include
random
avg
compare
Compare the value of two columns in the database. In comparison we use operators like =
, >
, <
, !=
.
like
Used to add a like condition to a query
notLike
Used to add a not like condition to a query
div
Used to divide a column by a certain value in the database
between
Adds a between check on a column. It checks if the value of the given column is between two given points.
notBetween
Checks if the value of the given column is not between
the given points.
jsonified
Jsonify the given value and assigns it to the given column.
of
Multiplies a column by a certain value in the database
minus
Subtracts a column by a certain value in the database
plus
Adds a column by a certain value in the database
eq
Opposite of eq. Checks if the value of the given column is equal
to the given value.
neq
Opposite of eq. Checks if the value of the given is not equal
to the given value.
now
Assigns the current timestamp to the given alias or column.
lt
Check if the column value is less than
the given value.
lte
Checks if the column value is less than or equal
to the given value.
gt
Checks if the column value is greater than
the given value.
gte
Checks if the column value is greater than or equal
the given value.
uuid
This can be used in two ways. The first way is where a uuid is provided and the other way is where you want to sign a unique random uuid to a column.
max()
Gets the maximum value of the given column and assigns it to the given alias
min()
Gets the minimum value of the given column and assigns it to the given alias
sum()
Gets the sum of the given column and assigns it to the given alias.
regex
If all above don’t work for you, you can use this aggregation function to provide your own regular expression that the db should check against.
Chaining multiple
You can chain as many aggregations as you with till you call the build()
method.