How do I group query results by the hour part of a datetime column in SQLAlchemy?
I didn't mention I'm using sqlite; date_trunc is postgres?
Dave
2010-01-21 23:04:27
A:
If I remember correctly you must first extract the hour from your timestamp and then you can group by that.
query(extract('hour', timeStamp)).group_by(hour)
Scott
2010-01-21 22:34:47
A:
You can also do it in Python. Assuming you have an ordered query_result
:
from itertools import groupby
def grouper( item ):
return item.created.hour
for ( hour, items ) in groupby( query_result, grouper ):
for item in items:
# do stuff
This answer is adapted from an answer to a similar question here
Raben
2010-09-25 22:11:20