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.

order_by aggregated fields don't use filter

See original GitHub issue

Hello,

I’m having an issue while using order_by on aggregate fields.

{
  author (order_by: {articles_aggregate: { count: desc }}) {
    id
    name
    articles_aggregate(where: $where) {
        aggregate {
            count
        }
    }      
  }
}

It looks like the SQL query doesn’t apply the filter used in the aggregated count on the order_by count. Am I doing something wrong?

#1039 #1042

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:18
  • Comments:20 (1 by maintainers)

github_iconTop GitHub Comments

23reactions
0x777commented, Jan 10, 2019

@rafaelugolini This is currently the expected behavior. The filters used on *_aggregate fields in the query are not applied at the top level. For example, you can have such a query:

{
  author (order_by: {articles_aggregate: { count: desc }}) {
    id
    name
    count1: articles_aggregate(where: $where1) {
        aggregate {
            count
        }
    }    
    count2: articles_aggregate(where: $where2) {
        aggregate {
            count
        }
    }      
  }
}

In this case, we wouldn’t know which filter to apply at the top level.

Your use case however is very valid. Maybe we can extend the order_by syntax to allow specifying the where clause?

{
  author (order_by: {articles_aggregate: { where: $where using: {count: desc} }) {
    id
    name
    articles_aggregate(where: $where) {
        aggregate {
            count
        }
    }      
  }
}
11reactions
karibertilscommented, Oct 21, 2019

Any news on this ?

I am working with custom reports where the filters are quite dynamic and it is not possible to solve my case with views.

Having the possibility to add where filter to the order_by clause like @0x777 mentioned would solve this perfectly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SQL: How to filter after aggregation? - Stack Overflow
It is very easy to remove values that you don't want aggregated. For instance:
Read more >
GROUP BY and FILTER | SQL Tutorial Documentation on data ...
An introduction to the GROUP BY clause and FILTER modifier. GROUP BY enables you to use aggregate functions on groups of data returned...
Read more >
The FILTER clause: Selective Aggregates - Modern SQL
The FILTER clause extends aggregate functions (SUM, AVG, COUNT, etc.) with an additional WHERE clause. Databases not supporting FILTER can use CASE within ......
Read more >
Aggregating and Grouping Data
Filter and order results of a query based on aggregate functions. ... GROUP BY tells SQL what field or fields we want to...
Read more >
Filter, Search, Sort, Aggregate, Joins & More! - YouTube
The Query All Records function is a powerful and flexible function in Xano. It allows you to a wide-range of things when querying...
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