如何从Sequelize.js的最后几个月获得一年的总和?

时间:2016-11-16 07:06:39

标签: node.js sequelize.js

我有桌子每天计算用户评分。现在我必须得到每月最后一行的汇总表,并为所有用户相互汇总 不使用SQL查询怎么做?

1 个答案:

答案 0 :(得分:0)

我当前的解决方案很糟糕,但除了计算点数

外,它正在运行
models.users_rating
  .findAndCountAll({
    group: 'userId',
    limit: 30,
    offset: offset,
    where: where
  })
  .then(function(users){
    models.sequelize.query(`SELECT u.userId, u.login, SUM(u.win) as win, SUM(u.loose) as loose, 
      SUM(u.refund) as refund, SUM(u.summary) as summary, AVG(u.profitPercent) as profitPercent, (SUM(u.win) * 100 / (SUM(u.summary)-SUM(u.refund))) as prohod, (SUM(u.profitSv) * 100) / 5000 as profitPercentAll
      FROM users_ratings as u 
          INNER JOIN
          (
              SELECT *, MAX(rateDate) lastDate, substring(rateDate, 1, 7) monthly
              FROM users_ratings
              GROUP BY userId, monthly
          ) d ON u.userId = d.userId AND
                  u.rateDate = d.lastDate
      GROUP BY u.userId
      ORDER BY `+order+" LIMIT 30 OFFSET "+offset)
    .then(function(data){
      users.rows = data[0];
      return res.json(users);
    })
  });