Python SQLAlchemy中的独特选择和最大值

Python SQLAlchemy中的独特选择和最大值,python,sqlite,sqlalchemy,pyramid,Python,Sqlite,Sqlalchemy,Pyramid,我有下表: id | user_id | name | value ---------------------------- 1 | 1 | so | 1 2 | 1 | os | 2 3 | 2 | so | 3 4 | 2 | os | 4 5 | 2 | os | 7 我如何使用sqlalchemy在列表中选择name==“os”和max(value)where值的不同用户行 另外,可能没用,信息:

我有下表:

id | user_id | name | value
----------------------------
1  |       1 | so   | 1
2  |       1 | os   | 2
3  |       2 | so   | 3
4  |       2 | os   | 4
5  |       2 | os   | 7
我如何使用sqlalchemy在列表中选择name==“os”和max(value)where值的不同用户行

另外,可能没用,信息:我正在使用sqlalchemy和pyramid

这与我的尝试类似:

ResVar = DBSession.query(tableModel.id, tableModel.user_id, tableModel.name, tableModel.value, func.max(tableModel.value)).filter(tableModel.value.in_(list(checkList))).filter(tableModel.name == "so").group_by(tableModel.user_id).all()
这与我的模型相似:

class tableModel(Base):
    __tablename__ = 'table'
    id = Column(Integer,primary_key=True)
    name = Column(String(100))
    value = Column(String(200))
    user_id = Column(Integer,ForeignKey('user.id'))
    user = relationship("user",foreign_keys=[user_id])

    def __init__(self, user_id,name,value):
        self.user_id = user_id
        self.name = name
        self.value = value
预期结果:

1 |    1 | so  | 1
3 |    2 | so  | 3

由于SQLite不支持
上进行区分,因此我们必须使用
按用户id分组

T = tableModel
q = session.query(T.id, T.user_id, T.name, func.max(T.value)) \ 
           .filter(T.name=='so') \
           .filter(T.value.in_([1, 2, 3, 4])) \
           .group_by(T.user_id)
产生以下行:

(1, 1, u'so', u'1')
(3, 2, u'so', u'3')

由于SQLite不支持
上进行区分,因此我们必须使用
按用户id分组

T = tableModel
q = session.query(T.id, T.user_id, T.name, func.max(T.value)) \ 
           .filter(T.name=='so') \
           .filter(T.value.in_([1, 2, 3, 4])) \
           .group_by(T.user_id)
产生以下行:

(1, 1, u'so', u'1')
(3, 2, u'so', u'3')

你能附上你的模型类的源代码并编写你到目前为止尝试的代码吗?@Mikkoohtama我已经添加了模型和查询。还需要什么吗?您是否可以根据示例表数据添加示例结果行。请附上模型类的源代码,并编写您目前尝试的代码?@Mikkoohtama我已经添加了模型和查询。还需要什么吗?是否可以根据示例表数据添加示例结果行。