Skip to content

equivalent for .subquery('t2') sqlalchemy #1504

Answered by alhoo
movaldivia asked this question in Questions
Discussion options

You must be logged in to vote

You could try to execute the two queries in one exec call

        subq = (
            select(User.user_id, func.max(User.created_at).label("maxdate"))
            .group_by(User.user_id)
            .subquery("t2")
        )

        query = session.exec(
            select(User).join(
                subq,
                and_(User.user_id == subq.c.user_id, User.created_at == subq.c.maxdate),
            )
        ).all()

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@YuriiMotov
Comment options

Answer selected by YuriiMotov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
3 participants
Converted from issue

This discussion was converted from issue #139 on August 12, 2025 20:32.