Class AggregateFunctionBuilder<DB, TB, O>

An expression with an as method.

Type Parameters

  • DB

  • TB extends keyof DB

  • O = unknown

Hierarchy

  • AggregateFunctionBuilder

Implements

Constructors

Methods

  • Returns an aliased version of the function.

    In addition to slapping as "the_alias" to the end of the SQL, this method also provides strict typing:

    const result = await db
    .selectFrom('person')
    .select(
    (eb) => eb.fn.count<number>('id').as('person_count')
    )
    .executeTakeFirstOrThrow()

    // `person_count: number` field exists in the result type.
    console.log(result.person_count)

    The generated SQL (PostgreSQL):

    select count("id") as "person_count"
    from "person"

    Type Parameters

    • A extends string

    Parameters

    • alias: A

    Returns AliasedAggregateFunctionBuilder<DB, TB, O, A>

  • Adds a filter clause with a nested where clause after the function, where both sides of the operator are references to columns.

    Similar to WhereInterface's whereRef method.

    Examples

    Count people with same first and last names versus general public:

    const result = await db
    .selectFrom('person')
    .select((eb) => [
    eb.fn
    .count<number>('id')
    .filterWhereRef('first_name', '=', 'last_name')
    .as('repeat_name_count'),
    eb.fn.count<number>('id').as('total_count'),
    ])
    .executeTakeFirstOrThrow()

    The generated SQL (PostgreSQL):

    select
    count("id") filter(where "first_name" = "last_name") as "repeat_name_count",
    count("id") as "total_count"
    from "person"

    Type Parameters

    • LRE extends string | Expression<any> | DynamicReferenceBuilder<any> | SelectQueryBuilderExpression<Record<string, any>> | OperandExpressionFactory<DB, TB, any>

    • RRE extends string | Expression<any> | DynamicReferenceBuilder<any> | SelectQueryBuilderExpression<Record<string, any>> | OperandExpressionFactory<DB, TB, any>

    Parameters

    Returns AggregateFunctionBuilder<DB, TB, O>

  • Adds an over clause (window functions) after the function.

    Examples

    const result = await db
    .selectFrom('person')
    .select(
    (eb) => eb.fn.avg<number>('age').over().as('average_age')
    )
    .execute()

    The generated SQL (PostgreSQL):

    select avg("age") over() as "average_age"
    from "person"

    Also supports passing a callback that returns an over builder, allowing to add partition by and sort by clauses inside over.

    const result = await db
    .selectFrom('person')
    .select(
    (eb) => eb.fn.avg<number>('age').over(
    ob => ob.partitionBy('last_name').orderBy('first_name', 'asc')
    ).as('average_age')
    )
    .execute()

    The generated SQL (PostgreSQL):

    select avg("age") over(partition by "last_name" order by "first_name" asc) as "average_age"
    from "person"

    Parameters

    Returns AggregateFunctionBuilder<DB, TB, O>

  • Creates the OperationNode that describes how to compile this expression into SQL.

    If you are creating a custom expression, it's often easiest to use the sql template tag to build the node:

    class SomeExpression<T> implements Expression<T> {
    toOperationNode(): OperationNode {
    return sql`some sql here`.toOperationNode()
    }
    }

    Returns AggregateFunctionNode

Generated using TypeDoc