Module

x/kysely_postgrs_js_dialect/mod.ts>kysely.AggregateFunctionBuilder#filterWhereRef

Kysely dialect for PostgreSQL using the Postgres.js client.
Latest
method kysely.AggregateFunctionBuilder.prototype.filterWhereRef
import { kysely } from "https://dotland.deno.dev/x/kysely_postgrs_js_dialect@v0.27.4/mod.ts";
const { AggregateFunctionBuilder } = kysely;

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 ReferenceExpression<DB, TB>
RRE extends ReferenceExpression<DB, TB>