如何分别从嵌套的包含模型中获取月份和年份?
我有2个模型,用户和类别
用户模型具有
userName
,phone
,category_id
和status
。类别模型具有
catName
,description
我想根据月份和年份对在特定类别下注册的用户数量进行分类。
预期输出:
"catName": "sports",
"stat": [
{
"month": 2,
"year": 2018
"count": 2
},
{
"month": 3,
"year": 2018
"count": 2
},...}]
我已经尝试过了
const test = await models.Category.findAll({
attributes: {
include: [
[models.sequelize.fn("COUNT", models.sequelize.col("User.id")), "Count"]]
},
include: [{
model: models.User, as:'User', attributes:
[[models.sequelize.fn('date_trunc', 'Month' 'Year', models.sequelize.col('created_at')), 'Month','Year'],]
}],
group: ['Category.id','Month','year']
})