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.

Error on aggregation column sorting

See original GitHub issue

Issue type:

[ ] question [x] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [ ] postgres [x] sqlite [ ] sqljs [ ] react-native

TypeORM version:

[x] latest [ ] @next [ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

Hi there! I’ve faced a problem while trying to sort query in repo by aggregation field. This is a piece of my code:

const queryBuilder = this.createQueryBuilder('article')
      .addSelect('min(item.price)', 'minPrice')
      .leftJoin('article.items', 'item')
      .leftJoin('item.itemInsertions', 'itemInsertion')
      .where('article.collectionId = :collectionId', {collectionId: collection.id})
      .orderBy('minPrice', 'ASC')
      .groupBy('article.code');

As you can see, i need a sorting by minPrice. Then i should add this to “select” section (i can’t add sorting “min(item.price)” without adding to select because there is createOrderByCombinedWithSelectExpression method - it’s looking for alias if it meet “.” symbol - BTW is that correct behaviour?) . OK, doesn’t matter, i’ve added it to select, go further. When i call .getMany() count query is called first. It returns entity id’s array, but you’ve removed groupBy from this query in #313 (groupBy() is called). Then id’s array is incorrect - it returns 1 record in my case because of aggregation function usage. Then the main repository query is called with returned id’s array - so the where clause is incorrect too - “WHERE id IN ($1)”, one id is here in my case, because count query is incorrect. I think that #313 issue was resolved incorrectly - we still need groupBy clause in count query. Sorry if i’m wrong. Thank you!

Regards, Nikolay

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:3
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
RDeluxecommented, Sep 3, 2019

Hello, sorry I completely missed that.

I added something like this

qb = [...] // query build on table "jobs" with alias "job"

// adding subquery on table "application" with a many-to-one relation to job
qb.addSelect(subQuery => {
  return subQuery
    .select(`COUNT(*)`, 'appcount')
    .from('application', 'app')
    .where('app."jobId" = job.id');
}, 'appcount');

// adding the order by
qb.addOrderBy(`appcount`, opt);
0reactions
aco711commented, Sep 3, 2019

@RDeluxe thanks a ton

Read more comments on GitHub >

github_iconTop Results From Across the Web

List_SortColumn error on calculated attribute in aggregate
I have an aggregate with a calculated attribute to left pad a text column so I can do a numeric like sort. The...
Read more >
Adding ORDER BY gives error "must appear in the group by ...
When aggregating, you cannot order by an un-aggregated column. There may be any number of different values in the same aggregated group of ......
Read more >
Aggregation = Sum; column sorting not working properly
I needed a table aggregation that groups and compresses certain same number ... Aggregation = Sum; column sorting not working properly.
Read more >
Ordering before aggregate function causes error
PSQLException: ERROR: column XXX must appear in the GROUP BY clause or be used in an aggregate function. From SQL point of view...
Read more >
Sorting by Aggregates - Exago
Ensure that your report has the appropriate aggregate formula in a group footer or header (v2019.2+) cell. The cell should return a numeric...
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