How do I filter out all groups with aggregate share totals of 0?
q = session.query(Trades.ticker, func.sum(Trades.shares))
g = q.group_by(Trades.ticker)
f = g.filter(func.sum(Trades.shares) != 0)
result = f.all()
This throws the error on line 3:
... sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) misuse of aggregate: sum() ...
A workaround is to remove the filter and use a list comprehension:
result = [x for x in g.all() if x[1] != 0.0]
But this is very slow compared to filtering a SQL query, because it loads all the data into memory as opposed to only loading the filtered data.
(EDITED to simplify.)