6

How would I get the total count of a limited result with SQLAlchemy. I am currently using Postgres so I know I can use windowed functions. I basically want to know how I would write the following in SQLAlchemy:

SELECT foo
  ,count(*) OVER() AS full_count
FROM   bar
ORDER  BY <some col>
LIMIT  <pagesize>
OFFSET <offset>
0

1 Answer 1

3

Something like:

select(
    [
        bar.c.foo,
        func.count().over().label('full_count'),
    ],
    ...
)

Ticket where this was introduced: http://www.sqlalchemy.org/trac/ticket/1844#comment:9

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! That's exactly what I was looking for. I would also add that the following imports were needed: from sqlalchemy.sql.expression import select and from sqlalchemy import func.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.