Wolverine wrote:
> I have a question regarding counting repetitive elements in the
> table. I tried to do this on my own, but I'm hopeless.
>
> If I have a field like for example "city" how can I count elements
> based on this field?
The proper place to do research for this would actually be in the
SQLAlchemy documentation, which can be found here:
http://sqlalchemy.org/docs
This is one of those places where a knowledge of SQL is really going
to be required for you to be able to make any sort of progress :)
That being said, here is a solution to your question:
from elixir import *
from sqlalchemy import select, func
class Resident(Entity):
person = Field(String(64))
city = Field(String(64))
setup_all()
metadata.bind = 'sqlite:///'
metadata.create_all()
Resident(person='1', city='Paris')
Resident(person='2', city='Munchen')
Resident(person='3', city='Berlin')
Resident(person='4', city='New York')
Resident(person='5', city='Paris')
Resident(person='6', city='Barcelona')
Resident(person='7', city='Berlin')
Resident(person='8', city='Paris')
Resident(person='9', city='New York')
Resident(person='10', city='Los Angeles')
session.commit()
results = select(
[Resident.table.c.city, func.count(Resident.table.c.city)]
).group_by(Resident.table.c.city).execute()
for result in results:
print result
I hope this helps you.
--
Jonathan LaCour
http://cleverdevil.org
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"SQLElixir" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/sqlelixir?hl=en
-~----------~----~----~----~------~----~------~--~---