13
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

sqlalchemyでgroup_byしてsum

Last updated at Posted at 2014-01-30

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)

とか

13
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?