Skip to content

[Feature Request] Postgres DISTINCT ON supportΒ #604

@FlightOfStairs

Description

@FlightOfStairs

We would like to be able to express Postgres' SELECT DISTINCT ON construct in the ktorm DSL.

Example use-case is below (fiddle):

CREATE TABLE users (id VARCHAR, version BIGINT, data VARCHAR);

INSERT INTO users (id, version, data) VALUES
  ('id-1', 1, 'foo'),
  ('id-1', 2, 'bar'),
  ('id-2', 1, 'alpha'),
  ('id-2', 2, 'beta'),
  ('id-2', 3, 'gamma');

SELECT DISTINCT ON (id) id, data FROM users ORDER BY id, version DESC;

Results should be the latest for each user, i.e.,

id-1,bar
id-2,gamma

Currently we are doing two round trips to first fetch the set of latest versions, then fetch only those versions. Subquery support would also allow a single trip.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions