sqlalchemy:从表中获取最大值/最小值/平均值

sqlalchemy:从表中获取最大值/最小值/平均值

问题描述:

我有这个查询:

mps =   (
            session.query(mps)  .filter_by(idc = int(c.idc))
                                .filter_by(idmp = int(m.idmp))
                                .group_by(func.day(mps.tschecked))
        ).all()

我的问题是,我不知道如何(使用 sqlalchemy)从表中提取最大值/最小值/平均值...

My problem is, that I don't know how to extract (with sqlalchemy) the max/min/avg value from a table...

我发现这个:SQLAlchemy 中与数据库无关的 MAX() 函数

但是我不知道在哪里使用这个 func.max/min/avg...

But I don't know where to use this func.max/min/avg...

谁能告诉我怎么做?可以举个例子吗?

Can someone tell me how to do this? Can you give me an example?

from sqlalchemy import func 提供以下功能:

  • func.min
  • func.max
  • func.avg

文档在这里可用.

您可以在 query() 方法中使用它们.

You can use them i.e. in the query() method.

示例:

session.query(self.stats.c.ID, func.max(self.stats.c.STA_DATE))

(就像你在普通 SQL 中使用 agragate 函数一样)

(just like you use agragate functions in plain SQL)