Skip to main content

AggregateFunctionBuilder

AggregateFunctionBuilder is returned by aggregate function calls from the FunctionModule. It provides methods for adding modifiers like distinct, filter, order by, and over clauses to aggregate functions.

Type Parameters

DB
object
required
The database schema type
TB
keyof DB
required
The table references available in the current query context
O
any
default:"unknown"
The output type of the aggregate function

Methods

as

Returns an aliased version of the function.
alias
string
required
The alias name for the aggregate function result
In addition to slapping as "the_alias" to the end of the SQL, this method also provides strict typing. Example:

distinct

Adds a distinct clause inside the function.
Example:
The generated SQL (PostgreSQL):

orderBy

Adds an order by clause inside the aggregate function.
expr
OrderByExpression
required
Column or expression to order by
modifiers
OrderByModifiers
Optional order modifiers (e.g., asc, desc, nulls handling)
Example:
The generated SQL (PostgreSQL):

clearOrderBy

Removes all order by clauses from the aggregate function.

withinGroupOrderBy

Adds a within group clause with a nested order by clause after the function.
expr
OrderByExpression
required
Column or expression to order by
modifiers
OrderByModifiers
Optional order modifiers
This is only supported by some dialects like PostgreSQL or MS SQL Server. Example: Most frequent person name:
The generated SQL (PostgreSQL):

filterWhere

Adds a filter clause with a nested where clause after the function.
lhs
ReferenceExpression
required
Left-hand side column reference
op
ComparisonOperatorExpression
required
Comparison operator
rhs
OperandValueExpressionOrList
required
Right-hand side value or expression
expression
ExpressionOrFactory
Alternative: full expression or factory function
Example: Count by gender:
The generated SQL (PostgreSQL):

filterWhereRef

Adds a filter clause with a nested where clause after the function, where both sides of the operator are references to columns.
lhs
ReferenceExpression
required
Left-hand side column reference
op
ComparisonOperatorExpression
required
Comparison operator
rhs
ReferenceExpression
required
Right-hand side column reference
Example: Count people with same first and last names versus general public:
The generated SQL (PostgreSQL):

over

Adds an over clause (window functions) after the function.
over
OverBuilderCallback
Optional callback that returns an OverBuilder with partition by and order by clauses
Example: Simple window function:
The generated SQL (PostgreSQL):
With partition and order by:
The generated SQL (PostgreSQL):

$call

Simply calls the provided function passing this as the only argument. $call returns what the provided function returns.
func
function
required
Function to call with this builder as argument

$castTo

Casts the expression to the given type.
C
type parameter
required
The new output type
This method call doesn’t change the SQL in any way. This methods simply returns a copy of this AggregateFunctionBuilder with a new output type.

$notNull

Omit null from the expression’s type.
This function can be useful in cases where you know an expression can’t be null, but Kysely is unable to infer it. This method call doesn’t change the SQL in any way. This methods simply returns a copy of this with a new output type.

toOperationNode

Converts the builder to an operation node for internal use.

AliasedAggregateFunctionBuilder

AggregateFunctionBuilder with an alias. The result of calling AggregateFunctionBuilder.as.
DB
object
required
The database schema type
TB
keyof DB
required
The table references
O
any
The output type
A
string
The alias name

OverBuilderCallback

Callback type for the over method.