I have ticketing system with ticket status. I want to do sequelize query which will return respective status counts. Below is the raw query which is working fine, I want to convert it into sequelize.
let querydata = `SELECT count(*) as total,
sum(case when status = 'Allocated' then 1 else 0 end) as allocated,
sum(case when status = 'RemindMe' then 1 else 0 end) as remindme,
sum(case when status = 'Close' then 1 else 0 end) as closed
FROM ticketnotifications
WHERE createdby = '` + req.auth.credentials.email + `';`;
return ticketdb.sequelize.query(querydata, { type: Sequelize.QueryTypes.SELECT })
.then((agentCountResult) => {
return res(agentCountResult[0]);
}).catch((err) => {
logger.error('agentCount exception...', err);
return res(boom.expectationFailed(err));
});
I tried [Sequelize.fn('COUNT', Sequelize.col('id')), 'total']
and its working but I'm stucked around that how to convert above Case
statement in sequelize. Any idea?