graphql-engine icon indicating copy to clipboard operation
graphql-engine copied to clipboard

Feature request: Support aggregate(count, max, min...) in where clause

Open revskill10 opened this issue 6 years ago • 38 comments

Currently, there's no support for aggregate fields in where and order_by clause, which means it's impossible to do filtering on aggregation.

Suggestion: Support aggregated fields on where and order_by clause when we query from a table_aggregate.

Example:

query {
users_aggregate {
aggregate(where: {count: {_gt: 0} }) 
  { 
    count 
  }
}
}

or

query {
users_aggregate(where: {aggregate {count: {_gt: 0} }})  {
aggregate
  { 
    count 
  }
}
}

revskill10 avatar Jan 28 '19 14:01 revskill10

This feature would be huge for me!

phated avatar Mar 04 '19 20:03 phated

I keep running into scenarios where this is required.

karibertils avatar Jun 14 '19 16:06 karibertils

Any news on this? This seems like a big deal, and not terribly hard to translate to sql under the hood:

SELECT COUNT(*)
FROM blah
WHERE foo = bar

jkossis avatar Oct 29 '19 18:10 jkossis

Adding a voice of support here. You can already do aggregate queries with a where clause inside of the object.

investigation {
  investigation_history_aggregate(where: {interaction: {_eq: "added_note"}}) {
    aggregate {
      max {
        created_on
      }
    }
  }
}

Found it weird that you couldn't do that in the order/where clause.

hellsan631 avatar Jan 30 '20 15:01 hellsan631

Use case:

Fetch a list of authors (who have published more than 10 articles ever) and their articles (published between dates a and b). The list of authors is to be sorted based on the count of articles published between dates a and b

dsandip avatar Feb 17 '20 10:02 dsandip

Really need this, for instance if I'm doing a search and I still want to paginate the results I need to return the count of the searched items, not the whole table.

stefan1968 avatar Jun 12 '20 21:06 stefan1968

+1 again from me.

It's already possible to do very complex queries and filtering with awesome results. But as soon as it comes to sorting the results it is impossible. In many cases there is no good workaround and you need to bypass Hasura.

karibertils avatar Jun 12 '20 23:06 karibertils

@rikinsk @shahidhk @rakeshkky @wawhal @arvi3411301

Can we please pick this up next? There seems to be huge demand for this 🚀

Aditya94A avatar Jun 23 '20 05:06 Aditya94A

Does anybody know any temporary hacks to get this done? I really don't want to stink up my project with raw SQL scattered about (which hasura was supposed to solve completely 😋)

Aditya94A avatar Jun 23 '20 05:06 Aditya94A

Currently I am using this kind of work around for aggregation inside of query. Hope it helps.

query statistics {
  FindOutFrom(where:{ Patients: {id: { _is_null: false }}}) {
    name,
    Patients_aggregate {
      aggregate {
        count
      }
    }
  }
}

erikasakire avatar Jul 08 '20 10:07 erikasakire

+1 here

I need this feature please.

groksrc avatar Jul 14 '20 16:07 groksrc

Echoing this! Would be super useful. Have run into needing this several times now.

jondashkyle avatar Aug 02 '20 18:08 jondashkyle

+1 would be really useful!

marcfalk avatar Sep 07 '20 13:09 marcfalk

+1

aotradovec avatar Sep 11 '20 06:09 aotradovec

+1 Looking to be able to achieve multiple counts based off of different where clauses in a single aggregation query. The following workaround is terribly non performant.

query MyQuery($input_start_date: date, $input_end_date: date) {
  
  count1: keyword(args: {input_end_date: $input_end_date, input_start_date: $input_start_date}, where: {bid_diff: {_gt: "0"}}) {
    aggregate {
      count
      
    }
  }
  count2: keyword(args: {input_end_date: $input_end_date, input_start_date: $input_start_date}, where: {bid_diff: {_lt: "0"}}) {
    aggregate {
      count
      
    }
  }
  count3: keyword(args: {input_end_date: $input_end_date, input_start_date: $input_start_date}) {
    aggregate {
      count
      avg {
        recommended_bid
        bid_diff
      }
    }
    
  }
}

Would be nice if in the query itself you could add *count to the end of where clauses or at the end of a query to get aggregation totals.

keyword(args: {input_end_date: $input_end_date, input_start_date: $input_start_date}, where: {bid_diff: {_lt: "0"} *count: bid_diff_count }) {
    data
    *count // as total aggregate count
    *bid_diff_count
  }

adventurini avatar Oct 02 '20 21:10 adventurini

+1

jonathan-bibas avatar Jun 30 '21 21:06 jonathan-bibas

+1

lakhansamani avatar Jul 06 '21 08:07 lakhansamani

+1

machouz avatar Jul 06 '21 08:07 machouz

+1

karibertils avatar Jul 06 '21 14:07 karibertils

Ended up having to use a view with aggregations, and then setting up a relation.

Ideally we get proper support.

icodeforlove avatar Jul 24 '21 21:07 icodeforlove

+1 Also really need this!

12bchl avatar Sep 30 '21 21:09 12bchl

+1 too! This feature would be amazing

ZhiMing96 avatar Oct 11 '21 04:10 ZhiMing96

+1 this would really help

paulzakin avatar Oct 12 '21 19:10 paulzakin

+1 really need this. This would really help.

wackyapps avatar Dec 30 '21 15:12 wackyapps

+1, that would be super cool

KillianGDK-FDTI avatar Jan 06 '22 11:01 KillianGDK-FDTI

+1, would be a massive improvement to server side pagination with filters on aggregate fields

austin-coffman avatar Jan 11 '22 16:01 austin-coffman

+1, Need this really bad

ArzamastsevVladyslav avatar Jan 26 '22 09:01 ArzamastsevVladyslav

What's the standing on this issue @Hasura? The feature request is widely wanted. Would love to use it, thanks.

WGTW avatar Feb 02 '22 09:02 WGTW

Hello @revskill10 or anyone interested in this feature!

For further clarification of the request would this query also be an example of what you're looking for?

author (where: {articles_aggregate: {count: {_gt: 5}}) { id name }

Plus if there ae any other additional user story level example that would be very helpful.

One clarifying question: Are you considering querying the aggregate AND what is inside the aggregate?

Thanks!

dennislaviolette avatar Feb 17 '22 14:02 dennislaviolette

@dennislaviolette Yes, it's one of the query that i want.

The goal, is to build a search interface which shows facet statistics, for example, in an ecommerce, in search result, we want to show categories (count) of the search result.

You can take a look at amazon search result and its facet search on the left sidebar as a reference.

Thanks !

revskill10 avatar Feb 17 '22 19:02 revskill10