postgre实现按年月日周统计分组统计

  |  

const sequelize = require(‘sequelize’);

postgre 按照年分组统计

SELECT to_char(“createdAt”::DATE, ‘YYYY’) AS “year”, SUM(“money”) AS “total” FROM “reward_logs” AS “reward_log” GROUP BY “year” ORDER BY “year” DESC

1
2
3
4
5
6
7
8
9
(async () => {
let list = await model.findAll({
'attributes': [[ sequelize.literal(`to_char("createdAt"::DATE, '${'YYYY'}')`), 'year' ], [ sequelize.fn('SUM', sequelize.col('money')), 'total' ]],
'where': {},
'group': [ sequelize.col('year') ],
'order': [[ sequelize.col('year'), 'DESC' ]],
});
console.log('list===========>', list);
})();