from itertools import groupby

def grouper(item):
    return item.created.year, item.created.month
for (year, month), items in groupby(query_result, grouper):
    pass
group by year, month, day in a sqlalchemy