此查询使用 to_char abd group_by
从 yo 博客组中选择 to_char(date_published, 'yyyymm') yo;
我试过了
In [3]: year_field = db.func.extract('year', ArticleModel.date_published)
In [4]: month_field = db.func.extract('month', ArticleModel.date_published)
In [5]: yonth_field = year_field * 100 + month_field
In [6]: print db.session.query(yonth_field.label('yonth'), db.func.count(1)).group_by('yonth')
SELECT EXTRACT(year FROM article_meta.date_published) * :param_1 + EXTRACT(month FROM article_meta.date_published) AS yonth, count(:param_2) AS count_1
FROM article_meta JOIN article ON article_meta.id = article.id GROUP BY yonth
有简单的方法吗?