having

abstract fun having(clause: Clause.(T) -> Unit): SelectStatement<T>

Adds a HAVING clause to the SELECT statement to filter the results of GROUP BY.

The HAVING clause is used to filter groups created by the GROUP BY clause. It is similar to the WHERE clause, but WHERE filters rows before any grouping occurs, while HAVING filters groups after they have been created. Therefore, HAVING is almost always used with GROUP BY and aggregate functions.

This function can only be called once per SELECT statement. Subsequent calls will overwrite the previously defined HAVING clause.

Example

// Select departments and their average salary, but only for departments
// where the average salary is greater than 50000.
val cursor =
Employees
.select(Employees.departmentId, AVG(Employees.salary))
.groupBy(Employees.departmentId)
.having { AVG(Employees.salary) GT 50000.0 }
.execute()

// Process the results
cursor.forEach { row ->
val departmentId = row[Employees.departmentId]
val avgSalary = row[AVG(Employees.salary)]
println("Department $departmentId has an average salary of $avgSalary")
}

Return

The SelectStatement instance, allowing for further chaining of query-building methods.

Author

MOHAMMAD AZIM ANSARI

Parameters

clause

A lambda expression with a Clause receiver that defines the filtering conditions for the groups.

See also