Skip to main content

Avg

Definition

The avg method allows you to add the AVG function to the query. The AVG function allows you to calculate the average value of a set.

There are 2 ways to call this method:

1. Calling from a KColumn or a KTableColumn

The only one method available to use this functionality calling from a KColumn or a KTableColumn is:

  • avg(): It does not receive any parameters. The KColumn or KTableColumn that invokes the method will be the one supplied to the AVG function.

Example

Java code:

k
.select(
APP_USER.ID.avg(),
APP_USER.CREATED_AT.cast(date())
)
.from(APP_USER)
.groupBy(APP_USER.CREATED_AT.cast(date()))
.multiple();

SQL generated:

SELECT
AVG(au.id),
CAST(au.created_at AS DATE)
FROM app_user au
GROUP BY CAST(au.created_at AS DATE)

Parameters:

  • None

2. Calling from the KFunction class

The only one method available to use this functionality calling from the KFunction class is:

  • avg(KColumn kColumn): Receives a KColumn or a KTableColumn which will be supplied to the AVG function.

To use this way, you need to import the static functions as follows:

import static com.myzlab.k.KFunction.*;

Example

Java code:

k
.select(
avg(APP_USER.ID),
APP_USER.CREATED_AT.cast(date())
)
.from(APP_USER)
.groupBy(APP_USER.CREATED_AT.cast(date()))
.multiple();

SQL generated:

SELECT
AVG(au.id),
CAST(au.created_at AS DATE)
FROM app_user au
GROUP BY CAST(au.created_at AS DATE)

Parameters:

  • None