Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1.3K
Views
How to add SUM to nested query in typeorm?

I have 3 tables : CardTypeOne , Card , TransactionMoney . CardTypeOne has a foreign key to card table (OneToOne relation). the card table has 2 foreign key to TransactionMoney Table (OneToMany relations) like below:
CardTypeOne Table:

 @OneToOne(type => Card)
    @JoinColumn()
      card: Card;

Card Table is:

@OneToMany(
    () => TransactionMoney,
    transactionMoney => transactionMoney.fromCard,
  )
  outterTransaction: TransactionMoney[];

  @OneToMany(
    () => TransactionMoney,
    transactionMoney => transactionMoney.toCard,
  )
  innerTransaction: TransactionMoney[];

TransactionMoney Table:

 @Column('integer')
  amount: number;

  @ManyToOne(
    () => Card,
    card => card.withdraws,
  )
  outterTransaction: Card;

  @ManyToOne(
    () => Card,
    card => card.deposit,
  )
  innerTransaction: Card;

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:

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();

But when I run the code I got this error:

column "typeOne.id" must appear in the GROUP BY clause or be used in an aggregate function
over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!