title: How to add SUM to nested query in typeorm?
body: <p>I have 3 tables : <code>CardTypeOne</code> , <code>Card</code> , <code>TransactionMoney</code> .
  <code>CardTypeOne</code> has a <code>foreign key</code> to <code>card</code> table (<code>OneToOne</code> relation). the <code>card</code> table has 2 foreign key to <code>TransactionMoney</code> Table (<code>OneToMany</code> relations) like below:<br />
  <code>CardTypeOne</code> Table:</p>
  <pre><code> @OneToOne(type =&gt; Card)
      @JoinColumn()
        card: Card;
  </code></pre>
  <p>Card Table is:</p>
  <pre><code>@OneToMany(
      () =&gt; TransactionMoney,
      transactionMoney =&gt; transactionMoney.fromCard,
    )
    outterTransaction: TransactionMoney[];
  
    @OneToMany(
      () =&gt; TransactionMoney,
      transactionMoney =&gt; transactionMoney.toCard,
    )
    innerTransaction: TransactionMoney[];
  </code></pre>
  <p>TransactionMoney Table:</p>
  <pre><code> @Column('integer')
    amount: number;
  
    @ManyToOne(
      () =&gt; Card,
      card =&gt; card.withdraws,
    )
    outterTransaction: Card;
  
    @ManyToOne(
      () =&gt; Card,
      card =&gt; card.deposit,
    )
    innerTransaction: Card;
  </code></pre>
  <p>TransactionMoney table has an amount field. I want to calculate the sum function on the outterTransaction on the amount field, and do same for innertransaction. Finally I'd like to subtract these summation.
  here is my try code:</p>
  <pre><code>await getConnection()
        .getRepository(CardTypeOne)
        .createQueryBuilder('typeOne')
        .leftJoinAndSelect('typeOne.card', 'card')
        .leftJoin('card.outterTransaction','outter')
        .leftJoin('card.innerTransaction','inner')
        .addSelect('sum (outter.amount) as balance1')
        .addSelect('sum (inner.amount) as balance2')
        .where('typeOne.id= :id', { id: id})
        .getOne();
  </code></pre>
  <p>But when I run the code I got this error:</p>
  <pre><code>column &quot;typeOne.id&quot; must appear in the GROUP BY clause or be used in an aggregate function
  </code></pre>