node.js - Sequelize中如何使用CASE WHEN表达式?

标签 node.js sequelize.js

使用 Sequelize ORM for Node.js,如何在 select 语句中使用 CASE/WHEN 表达式?

我没有看到任何使用 SQL 表达式的引用或示例 CASE根据 Sequelize Documentation .这可能吗?

这是我的例子:

SQL

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

Sequelize

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 => {
  console.log(result);
});

数据库结构

+--------+------+----------+
| 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    |
+--------+------+----------+

期望的结果:

+--------+----------+--------+--------+-------+
| userId | Employee | School | Public | Other |
+--------+----------+--------+--------+-------+
| 1      | 2.00     | 3.50   | 4.00   | 2.50  |
+--------+----------+--------+--------+-------+
| 2      | 3.75     | 4.25   | 2.00   | 3.00  |
+--------+----------+--------+--------+-------+

最佳答案

您可以使用 Sequelize.literalattributes 中构建特殊查询

var query = {
  attributes: [
    'userId',
    [Sequelize.literal(`MAX(CASE Type WHEN 'Employee' THEN Rate ELSE 0 END)`), 'Employee'],
    [Sequelize.literal(`MAX(CASE Type WHEN 'School' THEN Rate ELSE 0 END)`), 'School'],
    [Sequelize.literal(`MAX(CASE Type WHEN 'Public' THEN Rate ELSE 0 END)`), 'Public'],
    [Sequelize.literal(`MAX(CASE Type WHEN 'Other' THEN Rate ELSE 0 END)`), 'Other'],
  ],
  where: {
    user: userId,
    Type: ['Employee', 'School', 'Public', 'Other'],
  },
  group: ['userId'],
  raw: true
};
models.invoice.findAll(query).then(result => {
  console.log(result);
});

关于node.js - Sequelize中如何使用CASE WHEN表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47396796/

相关文章:

javascript - OpenTok NodeJS 视频聊天

javascript - nodejs报错undefined is not a function

javascript - 在 Node.js cli 中从 Dropbox 下载文件

javascript - Node.js + ExpressJS + Socket.IO TypeError : obj. on is not a function

sql - Sequelize : Create instance with existing association

node.js - Sequelize : can attribute have value from relation attribute data?

javascript - 在 where 子句中续写 : use field created in sequelize. 文字

node.js - Nodejs中的异步递归是什么意思

javascript - Sequelize .sync() : error in SQL syntax near NUMBER

javascript - 获取错误连接 ECONNREFUSED 127.0.0.1 :3306 when start my node. js 应用程序