On Mar 27, 2008, at 4:56 AM, shabda raaj wrote:
> >>>> main_link = LinkVote.alias('main_link') >>>> peer = LinkVote.alias('peer') >>>> countr = LinkVote.alias('countr') >>>> conn.execute(select([main_link.c.link_id, peer.c.link_id, >>>> func.count(peer.c.user_id)/select([func.count(countr.c.id)], >>>> countr.c.link_id == peer.c.link_id, correlate = peer).scalar()], >>>> and_(peer.c.user_id == main_link.c.user_id, peer.c.direction == >>>> main_link.c.direction, main_link.c.link_id == 149), group_by = >>>> peer.c.link_id, limit = 30)) > Assume 0.4 usage here. The "scalar()" method there should be "as_scalar()" - "scalar()" indicates you'd like to execute the query immediately and return a single result. Also "correlate" is a boolean argument, not a selectable, and is "True" by default in any case...its a leftover from older versions of SA before we had the generative "correlate()" method. If you want to set correlations explicitly, use "select(...).correlate(peer)". But in this case its not needed since the correlation is straightforward: select([main_link.c.link_id, peer.c.link_id, func.count(peer.c.user_id)/select([func.count(countr.c.id)], countr.c.link_id == peer.c.link_id).as_scalar()], and_(peer.c.user_id == main_link.c.user_id, peer.c.direction == main_link.c.direction, main_link.c.link_id == 149), group_by = peer.c.link_id, limit = 30) produces: SELECT main_link.link_id, peer.link_id, count(peer.user_id) / (SELECT count(countr.id) AS count_1 FROM news_linkvote AS countr WHERE countr.link_id = peer.link_id) AS anon_1 FROM news_linkvote AS main_link, news_linkvote AS peer WHERE peer.user_id = main_link.user_id AND peer.direction = main_link.direction AND main_link.link_id = :main_link_link_id_1 GROUP BY peer.link_id LIMIT 30 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~----------~----~----~----~------~----~------~--~---