Skip to content

Instantly share code, notes, and snippets.

@mmautner
Created November 4, 2013 14:29
Show Gist options
  • Save mmautner/7303273 to your computer and use it in GitHub Desktop.
Save mmautner/7303273 to your computer and use it in GitHub Desktop.
example sqlalchemy ORM query
# get count of users by birthday who've clicked in the last 7 days
results = session.query(User)\
.join(Click, User.id == Click.user_id)\
.filter(Click.created_at > datetime.datetime.today() - datetime.timedelta(days=7))\
.groupby(User.birthday)\
.orderby(User.birthday)\
.values(User.birthday,
func.count(func.distinct(User.id)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment