I've got a table (story_id, votes) with data [(1,3), (2,4)]
when I try to do a query... session.query(table.c.story_id, func.max(table.c.votes)).first() i'll get: (1,4)
the result I expect is: (2,4)
where is the misunderstanding?
I've got a table (story_id, votes) with data [(1,3), (2,4)]
when I try to do a query... session.query(table.c.story_id, func.max(table.c.votes)).first() i'll get: (1,4)
the result I expect is: (2,4)
where is the misunderstanding?
You are not grouping by anything so the database can simply return any row with a query like this. If you would add a group_by=table.c.story_id than this would return the proper result.
Most databases would, because of this, block these queries by default since the returned result would be arbitrary. In PostgreSQL for example you would get an error that story_id is not part of an aggregate query and not in the group by clause so it wouldn't know what row to return.
Either way, try this: session.query(table.c.story_id, func.max(table.c.votes)).group_by(table.c.story_id).first()