Andrew_1510 Andrew_1510 - 3 months ago 33
SQL Question

sqlalchemy simple example of `sum`, `average`, `min`, `max`

For sqlalchemy, Who can gently give simple examples of

SQL
functions like
sum
,
average
,
min
,
max
, for a column (
score
in the following as an example).

As for this mapper:

class Score(Base):
#...
name = Column(String)
score= Column(Integer)
#...

van van
Answer

See SQL Expression Language Tutorial for the usage. The code below shows the usage:

from sqlalchemy.sql import func
qry = session.query(func.max(Score.score).label("max_score"), 
                    func.sum(Score.score).label("total_score"),
                    )
qry = qry.group_by(Score.name)
for _res in qry.all():
    print _res