2

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?

Vilas
  • 837
  • 2
  • 15
  • 41
  • 2
    Possible duplicate of [How to use CASE WHEN expression in Sequelize?](https://stackoverflow.com/questions/47396796/how-to-use-case-when-expression-in-sequelize) – Alexandr Zarubkin Aug 03 '19 at 06:52

0 Answers0