Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

1.3K
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda