0

I have the following query and I need to add it the func.count().over().label('full_count') like described by sayap in this post, but i absolutely don't know how :(.

Here is my query :

paginated_sql_query = "SELECT *, count(*) OVER() AS full_count FROM sim_cards " + sql_query

if per_page > 0:
   paginated_sql_query = paginated_sql_query + " LIMIT :limit OFFSET :offset"
   parameters["limit"] = per_page
   parameters["offset"] = (page - 1) * per_page

result_rows = session.execute(
   select(SimCards)
   .from_statement(text(paginated_sql_query)), parameters)
   .scalars()

I tried to pass an array on the select like that but without result :( :

result_rows = session.execute(
   select( [SimCards, func.count().over().label('full_count')] )
   .from_statement(text(paginated_sql_query)), parameters )
   .scalars()

My need is to have the total count in only one query with my paginated results and to know how to exploit it after.

6
  • dont paginate if you want the entire count. Also what is the error/wrong output you are getting ? Commented May 27, 2022 at 9:47
  • @leoOrion it seems that the count is the total one, not only the paginated one. My error is this is one : sqlalchemy.exc.NoSuchColumnError: Could not locate column in row for column 'count(*) OVER ()' Commented May 27, 2022 at 14:27
  • is there a clumn called full_count in the view being selected ? Commented May 30, 2022 at 10:02
  • When you say "the view" you mean SimCards in my case ? If yes, the answer is no because SimCards is SQLAlchemy Model and the full_count is a dynamic information created with the query Commented May 30, 2022 at 10:08
  • I mean in the columns that will appear after selection is done. It would help if you post the query. Commented May 30, 2022 at 10:11

0

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.