COUNT

fun COUNT(column: KQLiteColumn<*>? = null, distinct: Boolean = false): KQLiteColumn<Long>

Represents the SQL COUNT() aggregate function.

This function returns the number of rows in a group.

  • COUNT(*): (the default, when column is null): Returns the total number of rows in the group, including rows with NULL values.

  • COUNT(column): Returns the number of rows where the specified column is not NULL.

  • COUNT(DISTINCT column): Returns the number of distinct, non-NULL values in the specified column.

The result of COUNT() is always an INTEGER value, Kotlin Long.

Example

// SELECT COUNT(*) FROM Employees;
val cursor1 =
Employees
.select(COUNT())
.execute()
println(cursor1[COUNT()])
cursor1.close()

// SELECT COUNT(city) FROM Employees;
val cursor2 =
Employees
.select(COUNT(Employees.city))
.execute()
println(cursor2[COUNT(Employees.city)])
cursor2.close()

// SELECT COUNT(DISTINCT city) FROM Employees;
val distinctCities = COUNT(Employees.city, distinct = true)
val cursor3 =
Employees
.select(distinctCities)
.execute()
println(cursor3[distinctCities])
cursor3.close()

Return

A KQLiteColumn representing the count, which is always of type Long.

Author

MOHAMMAD AZIM ANSARI

Parameters

column

The column to count. If null, it performs a COUNT(*) which counts all rows. Defaults to null.

distinct

If true, only distinct (unique) non-NULL values in the column are counted. Defaults to false.

See also