[sqlalchemy] order_by with func.count

2010-06-15 Thread Marcin Krol

Hello,

I have this query:

rsvp = session.query(Project.project, 
func.count(Reservation.project_id)).join(Reservation.project).group_by(Project.project, 
Project.id).order_by(Project.project)


 print rsvp
SELECT project.project AS project_project, count(reservation.project_id) 
AS count_1
FROM reservation JOIN project ON project.id = reservation.project_id 
GROUP BY project.project, project.id ORDER BY project.project


So far so good - but what if I want to order by column 
func.count(Reservation.project_id)?


I can do this in SQL all right:

SELECT project.project AS project_project, count(reservation.project_id) 
AS count_1
FROM reservation JOIN project ON project.id = reservation.project_id 
GROUP BY project.project, project.id ORDER BY count_1 DESC


But how to do this in above sqla query?


--

Regards,
mk

--
Premature optimization is the root of all fun.

--
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



[sqlalchemy] order_by with func.count

2010-06-15 Thread Marcin Krol

Hello,

OK I figured this out:

rsvp = session.query(Project.project, Project.id, 
func.count(Reservation.project_id)).join(Reservation.project).group_by(Project.project, 
Project.id).order_by(desc(func.count(Reservation.project_id))).all()


I'm not normally a vaseline man, but this is amazing: how did SQLA guess 
 *correctly* what I wanted here? I love this toolkit!



--

Regards,
mk

--
Premature optimization is the root of all fun.

--
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.