Sum Distinct
Definition
The sumDistinct method allows you to add the SUM function (with DISTINCT option) to the query. The SUM function (with DISTINCT option) allows you to calculate the sum value of a set, considering only the distinct values.
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:
sumDistinct(): It does not receive any parameters. TheKColumnorKTableColumnthat invokes the method will be the one supplied to theSUMfunction.
Example
Java code:
k
.select(
APP_USER.ID.sumDistinct(),
APP_USER.CREATED_AT.cast(date())
)
.from(APP_USER)
.groupBy(APP_USER.CREATED_AT.cast(date()))
.multiple();
SQL generated:
SELECT
SUM(DISTINCT 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:
sumDistinct(KColumn kColumn): Receives aKColumnor aKTableColumnwhich will be supplied to theSUMfunction.
To use this way, you need to import the static functions as follows:
import static com.myzlab.k.KFunction.*;
Example
Java code:
k
.select(
sumDistinct(APP_USER.ID),
APP_USER.CREATED_AT.cast(date())
)
.from(APP_USER)
.groupBy(APP_USER.CREATED_AT.cast(date()))
.multiple();
SQL generated:
SELECT
SUM(DISTINCT au.id),
CAST(au.created_at AS DATE)
FROM app_user au
GROUP BY CAST(au.created_at AS DATE)
Parameters:
- None