11

I would like to group by date and count the resulting number of id's using sqlalchemy.

Unfortunately, my column containing date information created_datetime is a datetime and I would like to use an sql function like

group by date(created_datetime)

in order to group by date.

Here is what I have so far...

conn = engine.connect()

s = my_db.my_table.alias()

q = select([s.c.id]).\
    group_by(s.c.created_datetime).\
    count()

result = conn.execute(q)

for i in result:
    print(i)

conn.close()

1 Answer 1

12

Use sqlalchemy.sql.functions.func for this:

from sqlalchemy.sql import func

q = select([s.c.id]).\
    group_by(func.date(s.c.created_datetime)).\
    count()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.