SqlAlchemy(Postgres + Flask):如何求和多列?

维韦卡农

我有一个Score带有一列的类item_id和几个具有不同分数类型(score1,score2,score3 ...)的字段,它们都具有整数值。

我需要编写一个查询,获取分数类型列表,并返回一个列表,其中包含对象itemid以及列表中提到的所有分数类型的分数总和。我正在尝试使用混合方法来做到这一点,但对如何编写查询感到困惑。

模型

class Score(db.Model):

    __tablename__ = 'scores'

    item_id                     = db.Column(db.Integer(), primary_key=True)
    score1                      = db.Column(db.Integer(), nullable=False)
    score2                      = db.Column(db.Integer(), nullable=False)
    score3                      = db.Column(db.Integer(), nullable=False)
    score4                      = db.Column(db.Integer(), nullable=False)

    @hybrid_method
    def total_score(self, fields):
        ts = 0
        for field in fields : 
            ts = ts + self[field]
        return ts

controller.py

app.route('/scores', methods=['POST'])
def scores():
    fields = ['score1', 'score2']
    scores = Score.query.all().order_by('total_score')

显然这是行不通的。您能帮我写查询吗,非常感谢!

这就是我需要最终输出的方式:

[{'item_id' : 'x1', 'total_score' : y1},{'item_id' : 'x2', 'total_score' : y2},{'item_id' : 'x3', 'total_score' : y3}, ...]
rmn

您需要创建表达式hybrid_method

class Score(db.Model):
    __tablename__ = 'scores'
    item_id  = db.Column(db.Integer(), primary_key=True)
    score1 = db.Column(db.Integer(), nullable=False)
    score2 = db.Column(db.Integer(), nullable=False)
    score3 = db.Column(db.Integer(), nullable=False)
    score4 = db.Column(db.Integer(), nullable=False)

    @hybrid_method
    def total_score(self, fields):
        return sum(getattr(self, field) for field in fields)

    @total_score.expression
    def total_score(cls, fields):
        return sum(getattr(cls, field) for field in fields)


fields = ['score1', 'score2']
scores = db.session.query(Score.item_id, Score.total_score(fields).label('total_score')).order_by('total_score')
final_output = [score._asdict() for score in scores]

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

SqlAlchemy(Postgres + Flask):如何求和多列?

来自分类Dev

Postgres用作SQLAlchemy中的列

来自分类Dev

flask sqlalchemy使Postgres事务保持空闲

来自分类Dev

如何使用Postgres在SQLAlchemy中创建表?

来自分类Dev

如何在sqlalchemy中映射postgres EARTH列

来自分类Dev

SQLAlchemy为Postgres JSON列设置默认值

来自分类Dev

如何在SQLAlchemy中使用Postgres数值范围

来自分类Dev

Python SQLAlchemy和Postgres-如何查询JSON元素

来自分类Dev

如何使用sqlalchemy达到postgres最大连接数?

来自分类Dev

Flask-SQLAlchemy多对多

来自分类Dev

flask sqlalchemy postgres模型问题:如何避免手动输入“ id”

来自分类Dev

如何使用flask-sqlalchemy多次插入多对多关系?

来自分类Dev

uWSGI,Flask,sqlalchemy和Postgres:SSL错误:解密失败或记录Mac错误

来自分类Dev

postgres如何获取获取多列值?

来自分类Dev

如何在postgres中透视多列

来自分类Dev

Flask-SQLAlchemy查询多对多

来自分类Dev

使用Flask-SQLAlchemy选择多对多

来自分类Dev

Flask SQLAlchemy多对多删除元素

来自分类Dev

Flask-SQLAlchemy多对多插入

来自分类Dev

使用sqlalchemy,如何将1&0转换为postgres中的布尔值?

来自分类Dev

如何在 SQLAlchemy/Postgres 中限制每个 `group_by` 的 N 个结果?

来自分类Dev

如何使用sqlalchemy在子句中编写多列

来自分类Dev

SQLAlchemy + Flask多对一关系

来自分类Dev

SqlAlchemy Flask中的列参数

来自分类Dev

SqlAlchemy Flask中的列参数

来自分类Dev

如何使用SQLAlchemy对* count *子查询求和?

来自分类Dev

如何在Postgres的同一列中创建两列以求和?

来自分类Dev

如何对DataGridView的多列值求和

来自分类Dev

如何按多列条件求和(SQL)

Related 相关文章

热门标签

归档