> One query is ready but need to convert it into SQLAlchmey.
> Here is the query (it's working fine):
>
>    # select * from distributions inner join (select * from comments where
>    # comments.id in (select max(comments.id) as id from comments group by
>    # comments.distribution_id)) a on distributions.id = a.distribution_id
>    # order by a.datetime desc
>
> (The above logic is to find the latest feedback without duplicate distribution
>  names.  For example if a distribution has two recent feedback, I just
>  need latest among those. And I need to get total 5 latest feedbacks)
>

try this out:


from sqlalchemy.sql import func

stmt1 = session.query(func.max(Comments.id)).group_by(Comments.distribution_id)
stmt2 = session.query(Comments).filter(Comments.id.in_(stmt1)).subquery()

query = session.query(Distributions).join((stmt2,
                                          Distributions.id ==
stmt2.c.distribution_id)).order_by(
       stmt2.c.datetime)
print query.all()


Thanks,
Abdul Kader M
_______________________________________________
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers

Reply via email to