Skip to main content

Cardinality

Definition

The cardinality method allows you to add the CARDINALITY function to the query. The CARDINALITY function is used to returns the total number of elements in the array, or 0 if the array is empty.

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:

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

Example

Java code:

k
.select(
APP_USER.EMAIL.arrayAgg().cardinality(),
APP_USER.CREATED_AT.cast(date())
)
.from(APP_USER)
.groupBy(APP_USER.CREATED_AT.cast(date()))
.multiple();

SQL generated:

SELECT
CARDINALITY(ARRAY_AGG(au.email)),
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:

  • cardinality(KColumn kColumnArray): Receives a KColumn or a KTableColumn which will be supplied to the CARDINALITY 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(
cardinality(arrayAgg(APP_USER.EMAIL)),
APP_USER.CREATED_AT.cast(date())
)
.from(APP_USER)
.groupBy(APP_USER.CREATED_AT.cast(date()))
.multiple();

SQL generated:

SELECT
CARDINALITY(ARRAY_AGG(au.email)),
CAST(au.created_at AS DATE)
FROM app_user au
GROUP BY CAST(au.created_at AS DATE)

Parameters:

  • None