you might want to call correlate(User) on that scalar subquery also.  

paste the SQL along with it on the next email.


On Jun 24, 2013, at 1:15 PM, Sebastian Elsner <sebast...@risefx.com> wrote:

> Hm.... I'm lost. This is where I am now:
> 
> session.query(User.id, Assignment.id, 
> session.query(func.count(Assignment.id)).filter(Assignment.user_id == 
> User.id).as_scalar()).join(Assignment).all()
> 
> On 06/24/2013 04:26 PM, Michael Bayer wrote:
>> it's a scalar subquery.   you want to make the select() and then call 
>> as_scalar() on it so that it behaves like a column in a SQL expression.
>> 
>> 
>> On Jun 24, 2013, at 9:34 AM, Sebastian Elsner <sebast...@risefx.com> wrote:
>> 
>>> Hello,
>>> 
>>> I am trying to translate this SQL to a SQLAlchemy query, but failed so far:
>>> 
>>> select `users`.`name`, `assignments`.`id`,
>>>     (
>>>    select count(*)
>>>    from `assignments`
>>>     where `assignments`.`user_id` = `users`.`id`
>>>    ) as `num_assignments`
>>> from `users`
>>> join `assignments`
>>> on `assignments`.`user_id` = `users`.`id`
>>> 
>>> I would like to get results of (user_id, assignment_id, 
>>> total_assignments_per_user_id). I have found a similar question on the list 
>>> (https://groups.google.com/forum/#!topic/sqlalchemy/LBEyRe3w-8Q), and tried 
>>> to assemble a query like so, but I am missing something...
>>> 
>>> It would also be nice if there were a faster way to do this. Maybe someone 
>>> has a good idea.
>>> 
>>> And a question on terminology: is it really called subquery if the 
>>> "subquery" is in the "select block" (like above).
>>> 
>>> Many thanks
>>> 
>>> Sebastian
>>> 
>>> -- 
>>> You received this message because you are subscribed to the Google Groups 
>>> "sqlalchemy" group.
>>> To unsubscribe from this group and stop receiving emails from it, send an 
>>> email to sqlalchemy+unsubscr...@googlegroups.com.
>>> To post to this group, send email to sqlalchemy@googlegroups.com.
>>> Visit this group at http://groups.google.com/group/sqlalchemy.
>>> For more options, visit https://groups.google.com/groups/opt_out.
>>> 
>>> 
> 
> 
> -- 
> check out www.pointcloud9.com
> 
> Sebastian Elsner - Pipeline Technical Director - RISE
> 
> t: +49 30 20180300 flor...@risefx.com
> f: +49 30 61651074 www.risefx.com
> 
> RISE FX GmbH
> Schlesische Strasse 28, Aufgang B, 10997 Berlin
> c/o action concept, An der Hasenkaule 1-7, 50354 Hürth
> Geschaeftsfuehrer: Sven Pannicke, Robert Pinnow
> Handelsregister Berlin HRB 106667 B
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to sqlalchemy+unsubscr...@googlegroups.com.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/groups/opt_out.
> 
> 

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to