If I execute the following sequence code sequelize
models.Venta.sum( 'total' , { where: { fechaExpedicion: { [Op.gte]: '2018-03-31 00:00:00', [Op.lte]: '2018-03-31 23:59:59' } }, attributes: ['elaboradoPor'], group: 'elaboradoPor' , logging: console.log }) .then(totalIva => { console.log(JSON.stringify(totalIva)) }) .catch(e=> {console.log(e)})
I see only the first result of the survey (with sequelize). result sequelize code
With logging: console.log I get the SQL instruction for MariaDB:
SELECT elaboradoPor
, sum(total
) AS sum
FROM Venta
AS Venta
WHERE (Venta
.fechaExpedicion
>= '2018-03-31 00:00:00' AND Venta
.fechaExpedicion
<= '2018-03-31 23:59:59') GROUP BY elaboradoPor
;
If I execute, the select in HeidiSQL gives me the correct results. Select HeidySQL
Please, what is missing in the sequelize instruction to obtain the best results?
I changed the instruction and used findall () and fn to build the sums and integrated additional operations.
models.Venta.findAll({
where: {
fechaExpedicion: {
[Op.gte]: '2018-03-31 00:00:00',
[Op.lte]: '2018-03-31 23:59:59'
}
},
logging: console.log,
attributes: [
'elaboradoPor',
[Sequelize.fn('SUM', Sequelize.col('total')), 'totalSuma'],
[Sequelize.fn('SUM', Sequelize.col('totalIva')), 'totalIva'],
[Sequelize.fn('SUM', Sequelize.col('saldo')), 'totalSaldo']
],
group: ['elaboradoPor']
})
.then(resultados => {
console.log(JSON.stringify(resultados))
})
.catch(e => { console.log(e)
})
La respuesta es: sequelize final