6
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

posted at

updated at

sqlalchemyでgroup_byしてsum

PokoがModelとしてPoko.dateとかでgroup_byして、pvカラムをsumしたいみたいなとき

from sqlalchemy import func
q = session.query(Poko.date, func.sum(Poko.pv)).group_by(Poko.date)

for date, sum_pv in q:
    print date, sum_pv

みたいにタプルであつかえる

whereはさみたいときは

q = session.query(Poko.id, Poko.date, func.sum(Poko.pv)).filter_by(id=1).group_by(Poko.date)

みたいに

複数でgroup_byしたいとき

q = session.query(Poko.id, Poko.date, func.sum(Poko.pv)).group_by(Poko.date, Poko.id)

とか

Register as a new user and use Qiita more conveniently

  1. You can follow users and tags
  2. you can stock useful information
  3. You can make editorial suggestions for articles
What you can do with signing up
6
Help us understand the problem. What are the problem?