Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support DISTINCT ON #1040

Closed
dmoiseenko opened this issue Nov 15, 2018 · 6 comments · Fixed by #1099
Closed

Support DISTINCT ON #1040

dmoiseenko opened this issue Nov 15, 2018 · 6 comments · Fixed by #1099
Assignees
Labels
c/server Related to server e/quickfix can be wrapped up in few hours k/enhancement New feature or improve an existing feature p/medium non-urgent issues/features that are candidates for being included in one of the upcoming sprints

Comments

@dmoiseenko
Copy link

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

@0x777 0x777 added c/server Related to server e/quickfix can be wrapped up in few hours p/medium non-urgent issues/features that are candidates for being included in one of the upcoming sprints k/enhancement New feature or improve an existing feature labels Nov 15, 2018
rakeshkky added a commit to rakeshkky/graphql-engine that referenced this issue Nov 21, 2018
TODO:- Add tests and docs
@0x777
Copy link
Member

0x777 commented 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).

@dmoiseenko
Copy link
Author

@0x777
WOW! Works great!

@anonrose
Copy link

Thanks for the insight @0x777

@cleivson
Copy link

Hi, I know this has been closed already, but is it supposed to work with multiple columns like the postgres query does? I can only use it for a single column

@rakeshkky
Copy link
Member

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.

@jflambert
Copy link
Contributor

jflambert commented May 31, 2022

Hi, I know this has been closed already, but is it supposed to work with multiple columns like the postgres query does? I can only use it for a single column

you use [] to specify multiple columns. You can't do it with the graphiql editor.

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.

That's not at all what he was asking @rakeshkky he wanted to know about multiple columns in a distinct clause.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c/server Related to server e/quickfix can be wrapped up in few hours k/enhancement New feature or improve an existing feature p/medium non-urgent issues/features that are candidates for being included in one of the upcoming sprints
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants