question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support DISTINCT ON

See original GitHub issue

DISTINCT ON is a really powerful Postgres feature to perform aggregation-like(pick first in group). It can be used in plain SELECT queries without window function or GROUP BY. It will be great to see support of DISTINCT ON since Hasura has excellent support of ORDER BY.

e.g. We need to get the cheapest fruits in groups by color(see sudo SQL<=>GraphQL below)

SELECT DISTINCT ON
  (color)
  color,
  name,
  price
FROM
  fruit
ORDER BY price;
query {
  fruit(distinct_on: [color], order_by: {price: asc}) {
    color
    name
    price
  }
}

Postgres documentation https://www.postgresql.org/docs/11/sql-select.html#SQL-DISTINCT Usage example https://medium.com/statuscode/the-many-faces-of-distinct-in-postgresql-c52490de5954

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
0x777commented, Nov 22, 2018

@dmoiseenko Can you try this out on this PR build: https://hge-ci-pull-1099.herokuapp.com/

{
  product(distinct_on: color order_by: {color: asc price:desc}) {
    id
    price
    color
  }
}

Note that we force the Postgres restriction that the DISTINCT ON column(s) must match the leftmost ORDER BY column(s).

1reaction
rakeshkkycommented, Apr 18, 2022

Hi @cleivson, please note that this issue was related to Postgres. By the time this was closed, we only support single Postgres source (version 1.x). Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fixing DISTINCT ON Not Supported
Fixing DISTINCT ON Not Supported. Ever seen the following error when materializing a view? 1 SELECT DISTINCT ON is not supported. The DISTINCT...
Read more >
How to produce same output of "SELECT DISTINCT ON ...
PostgreSQL supports "DISTINCT ON" clause to eliminate rows that match on specified columns/expressions. It's not available on Snowflake, ...
Read more >
DISTINCT Clause | ClickHouse Docs
ClickHouse supports using the DISTINCT and ORDER BY clauses for different columns in one query. The DISTINCT clause is executed before the ORDER...
Read more >
Add support for select distinct on(a, b, c) - GitHub
The issue in Presto is that on one side, one can't use select distinct on (a, b) c from d but one also...
Read more >
MySQL 8.0 Reference Manual :: 8.2.1.18 DISTINCT Optimization
In most cases, a DISTINCT clause can be considered as a special case of GROUP BY . For example, the following two queries...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found