Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

310
Visualizações
sequelize - Specifying attributes for SELECT queries while using inner join

I want to fetch from the main table account and compare it with account history. there is a relationship On To many between the two table

SELECT account."userName", (account."postNumber" - accountHistory."postNumber") AS postNumber
    FROM public."Accounts" AS account  INNER JOIN public."AccountHistories" AS accountHistory
    ON account."userName" = accountHistory."userName" 
    WHERE accountHistory."scrappingDate" = '2022-01-08 23:59:39+01'
    ORDER BY postNumber DESC;

My question is how can I make the query with sequilize? I tried but I could not change the name of the attribute and do the subtraction

await this.accountRepository.findAll({    
    where: {'$accountHistory.createdAt$' : '2022-01-08 23:59:39+01'},
    include: [{
      model: AccountHistory,
      required: false
    }],
    attributes: ['userName',['$accountHistory.postNumber $', 'postNumber '],postNumber ],
   order: [[$accountHistory.createdAt$', 'DESC']],
                    })
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You probably need to use Sequelize.literal to perform the calculation as an aliased attribute, e.g.:

const { literal } = require('sequelize')

await this.accountRepository.findAll({    
    where: {'$accountHistory.createdAt$' : '2022-01-08 23:59:39+01'},
    include: [{
      model: AccountHistory,
      required: false
    }],
    attributes: [
      'userName',
      [
        literal('"account"."postNumber" - "accountHistory"."postNumber"'),
        'postNumber'
      ]
    ],
   order: [[$accountHistory.createdAt$', 'DESC']],
})

That exact syntax might not work depending on your SQL dialect - I'm not familiar with the $ symbols you are using

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda