如何在sequelize PostgreSQL中按周或按月排序?

时间:2019-01-28 11:54:04

标签: postgresql group-by count include sequelize.js

如何计算它们并每周或每月显示?

这是查询:

Company.findAll({
    attributes: [
        'id', 'title', 'is_enabled',
        [sequelize.fn('count', sequelize.col('company_users.id')) ,'user_count'] // <---- Here you will get the total count of user
    ],
    include: [
        {
            model: sqConn.CompanyUser,
            attributes: [] // <----- Make sure , this should be empty
        }
    ],
    group: ['companies.id'] // <---- You might require this one also
}).then(data => { 
    console.log(data); // <---- Check the output
})

预期输出: 公司详细信息以及

  

1。每月注册的公司用户数不存在。

     

2.year and month

"company": "data",
"company_user": [
  {
    "month": 2,
    "year": 2017,  
    "count": 2
  }, {
    "month": 3,
    "year": 2018,
    "count": 22  // number of company users registered in march 2018
  }, {...}
]

0 个答案:

没有答案