Skip to content

Instantly share code, notes, and snippets.

@relrod
Created August 12, 2013 20:06
Show Gist options
  • Save relrod/6214604 to your computer and use it in GitHub Desktop.
Save relrod/6214604 to your computer and use it in GitHub Desktop.
select persons.nickname, count(assertions.*) as assertions from assertions join persons on assertions.person_id = persons.id group by persons.nickname order by assertions desc limit 10;
@relrod
Copy link
Author

relrod commented Aug 12, 2013

Desired outcome is this:

tahrir=> select persons.nickname, count(assertions.*) as assertions from assertions join persons on assertions.person_id = persons.id group by persons.nickname order by assertions desc limit 10;
  nickname  | assertions 
------------+------------
 kevin      |         31
 adamwill   |         29
 cicku      |         29
 ralph      |         28
 spot       |         28
 limb       |         25
 remi       |         25
 orion      |         25
 pbrobinson |         25
 siwinski   |         24
(10 rows)

@lmacken
Copy link

lmacken commented Aug 12, 2013

I'm really not 100% on this one, but off the top of my head maybe something to this effect:

session.query(Person, func.count(Person.assertions)).join(Assertion).order_by(Person.assertions.desc()).limit(10).all()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment