title: TypeORM count grouping with different left joins each time
body: <p>I am using NestJS with TypeORM and PostgreSQL. I have a queryBuilder which joins other tables based on the provided array of relations.</p>
  <pre><code>const query = this.createQueryBuilder('user');
  
  if (relations.includes('relation1') {
    query.leftJoinAndSelect('user.relation1', 'r1');
  }
  if (relations.includes('relation2') {
    query.leftJoinAndSelect('user.relation2', 'r2');
  }
  if (relations.includes('relation3') {
    query.leftJoinAndSelect('user.relation3', 'r3');
  }
  
  // 6 more relations
  </code></pre>
  <p>Following that I select a count on another table.</p>
  <pre><code>query
  .leftJoin('user.relation4', 'r4')
  .addSelect('COUNT(case when r4.value &gt; 10 then r4.id end', 'user_moreThan')
  .addSelect('COUNT(case when r4.value &lt; 10 then r4.id end', 'user_lessThan')
  .groupBy('user.id, r1.id, r2.id, r3.id ...')
  </code></pre>
  <p>And lastly I use one of the counts (depending on the request) for ordering the result with orderBy.</p>
  <p>Now, of course, based on the relations parameter, the requirements for the groupBy query change. If I join all tables, TypeORM expects all of them to be present in groupBy.</p>
  <p>I initially had the count query separated, but that was before I wanted to use the result for ordering.</p>
  <p>Right now I planned to just dynamically create the groupBy string, but this approach somehow feels wrong and I am wondering if it is in fact the way to go or if there is a better approach to achieving what I want.</p>