title: How to use CASE WHEN expression in Sequelize?
body: <p>Using Sequelize ORM for Node.js, how do you use CASE/WHEN expression in a select statement?</p>
  
  <p>I do not see any reference or examples to use the SQL expression <a href="https://docs.microsoft.com/en-us/sql/t-sql/language-elements/case-transact-sql" rel="noreferrer">CASE</a> according to <a href="http://sequelize.readthedocs.io/en/v3/" rel="noreferrer">Sequelize Documentation</a> .  Is this possible?</p>
  
  <p>Here's my example:</p>
  
  <p><strong>SQL</strong></p>
  
  <pre><code>SELECT userId, Status, 
    MAX(CASE Type WHEN 'Employee' THEN Rate ELSE 0 END) AS "Employee",
    MAX(CASE Type WHEN 'School' THEN Rate ELSE 0 END) AS "School",
    MAX(CASE Type WHEN 'Public' THEN Rate ELSE 0 END) AS "Public",
    MAX(CASE Type WHEN 'Other' THEN Rate ELSE 0 END) AS "Other"
  FROM Database.dbo.invoice
  WHERE Status IN ('Included', 'Excluded')
  GROUP BY userId,  Status
  </code></pre>
  
  <p><strong>Sequelize</strong> </p>
  
  <pre><code>var query = {
    attributes: ['userId'], // need to include Rate (as Employee, School, Public, Other for each CASE/WHEN)
    /*
      MAX(CASE Type WHEN 'Employee' THEN Rate ELSE 0 END) AS "Employee",
      MAX(CASE Type WHEN 'School' THEN Rate ELSE 0 END) AS "School",
      MAX(CASE Type WHEN 'Public' THEN Rate ELSE 0 END) AS "Public",
      MAX(CASE Type WHEN 'Other' THEN Rate ELSE 0 END) AS "Other"
  */
    where: {
      user: userId,
      Type: ['Employee', 'School', 'Public', 'Other'],
    },
    group: ['userId'],
    raw: true
  };
  models.invoice.findAll(query).then(result =&gt; {
    console.log(result);
  });
  </code></pre>
  
  <p><strong>Database Structure</strong></p>
  
  <pre><code>+--------+------+----------+
  | userId | Rate | Type     |
  +--------+------+----------+
  | 1      | 2.00 | Employee |
  +--------+------+----------+
  | 1      | 3.50 | School   |
  +--------+------+----------+
  | 1      | 4.00 | Public   |
  +--------+------+----------+
  | 1      | 2.50 | Other    |
  +--------+------+----------+
  | 2      | 3.75 | Employee |
  +--------+------+----------+
  | 2      | 4.25 | School   |
  +--------+------+----------+
  | 2      | 2.00 | Public   |
  +--------+------+----------+
  | 2      | 3.00 | Other    |
  +--------+------+----------+
  </code></pre>
  
  <p><strong>Desired Result:</strong></p>
  
  <pre><code>+--------+----------+--------+--------+-------+
  | userId | Employee | School | Public | Other |
  +--------+----------+--------+--------+-------+
  | 1      | 2.00     | 3.50   | 4.00   | 2.50  |
  +--------+----------+--------+--------+-------+
  | 2      | 3.75     | 4.25   | 2.00   | 3.00  |
  +--------+----------+--------+--------+-------+
  </code></pre>