this should be a asked-before question, I searched but I could not find any answer on that. Sorry if it is duplicated. I have a query lets say:
my_query=session.query(Item).filter(somefilter)
Now, Item has a column, lets say counter
, and I want to find the sum of this column of my_query
.
I can do that like this:
sum=0
for row in query:
sum+=row.counter
but I don't this this is the efficient way of doing this specially in a large database. I know that this is possible: sqlalchemy simple example of `sum`, `average`, `min`, `max`, but this requires filtering on qry
(borrowed from the page) which I have already given the filtered version my_query
. I dont know if it is really more efficient to do the filtering again on top of qry
v.s. using the for loop on my_query
.