I'm very new to sqlalchemy and I'm still trying to wrap my head around
how it works.

I have a table with columns: type, amount.  I want to sum the amounts
grouped by type.  In SQL I would write:
SELECT sum(amount), type from purchases group by type;

How do I do this with SQLAlchemy?  This is what I have so far, but I
don't really understand what's going on:

pq = Session.query(Purchase).apply_sum(Purchase.amount)
for x in pq:
  ret += '<br>' + str(x.type) + str(x.amount)
  # This prints out every item in the db... the sum seems to not have
done anything

bytypes = pq.group_by(Purchase.type)
for x in bytypes:
  ret += '<br>' + str(x.type) + str(x.amount)
  # This prints out one of each type, but the amount is not the sum of
all the types, it's just the last one of each type

bytypes = bytypes.sum(Purchase.amount)
# This is the sum of everything.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to