oracle sql pagination with total pages or total entries

v1shnu

I'm trying to access paginated results from a DB. Following the below query:

SELECT * FROM
(
    SELECT a.*, rownum r__
    FROM
    (
        SELECT * FROM ORDERS WHERE CustomerID LIKE 'A%'
        ORDER BY OrderDate DESC, ShippingDate DESC
    ) a
    WHERE rownum < ((pageNumber * pageSize) + 1 )
)
WHERE r__ >= (((pageNumber-1) * pageSize) + 1)

from here , I am able to achieve pagination.

I modified the query like below inorder to get total counts as well :

SELECT * FROM
(
    SELECT a.*, rownum r__ , count(OrderDate)
    FROM
    (
        SELECT * FROM ORDERS WHERE CustomerID LIKE 'A%'
        ORDER BY OrderDate DESC, ShippingDate DESC
    ) a    group by OrderDate
) 
WHERE r__ between (pageNumber * pageSize )+ 1 and (pageNumber *pageSize) + pageSize;

But I also want to calculate the total number of pages and if the requested page is the last page as well. I tried to get the COUNT of the inner query so that I can calculate the total pages inside my code, but I could not get it. I've tried by COUNT(ORDERDATE) and grouping but it does not work.

Please tell me how I can achieve this.

Boneist

I would use analytic functions to do the work you're trying to do, e.g.:

SELECT res.*,
       CEIL(total_num_rows/pagesize) total_num_pages
FROM   (SELECT o.*,
               row_number() OVER (ORDER BY orderdate DESC, shippingdate DESC) rn,
               COUNT(*) OVER () total_num_rows
        FROM   orders o
        WHERE  customerid LIKE 'A%') res
WHERE  rn BETWEEN (pagenumber - 1) * pagesize + 1 AND pagenumber * pagesize;

N.B. untested.

The query does the pagination by using the row_number() analytic function to assign a number to each row, across the specified groups and in the specified order. Since we don't have a partition by clause in the OVER section, the function is working across the entire set of rows.

Similarly, we use the count() analytic function to get the count of the rows across the specified groups - and again, as we haven't specified any partition by clause, the function works across the entire set of rows.

Now you have those numbers, it's a simple matter to do the pagination and find the total number of pages.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pagination Current page/Total pages?

From Dev

Pagination Current page/Total pages?

From Dev

Reset total pages in jquery pagination plugin

From Dev

SQL Server 2008 pagination with total rows

From Dev

Running Total by Group SQL (Oracle)

From Dev

Decreasing running total in oracle sql

From Dev

Draper with Bootstrap Pagination - undefined method 'total_pages'

From Dev

angularjs + ui.bootstrap pagination wrong total pages number

From Dev

Problems with total number of pages in uib-pagination in angularJs

From Dev

Oracle SQL Aggregate minus Running Total

From Dev

Finding the total hits and visitors in sql Oracle

From Dev

Oracle SQL Aggregate minus Running Total

From Dev

SQL: Skip entries in an order without knowing total entry amount

From Dev

Selecting entries according to running total

From Dev

Get total number of results with pagination

From Dev

Laravel PHP: Selected Category total changes/resets to '0' when using additional pagination pages

From Dev

category & pagination issue NoMethodError in Articles#index undefined method `total_pages'

From Dev

Oracle Sql: How to add multi sub total in sql?

From Dev

Oracle Sql: How to add multi sub total in sql?

From Dev

Calculate total time in oracle

From Dev

Oracle SQL: How to determine total ranking using cursor with IN and OUT params

From Dev

Oracle SQL add colum with the total grouped by other field

From Dev

SQL sub total and final total query

From Dev

SQL sub total and final total query

From Dev

SQL - percentage of total rows

From Java

SQL dynamic running total

From Dev

sql previous week total

From Dev

SQL: Total days in a month

From Dev

SQL percentage of the total

Related Related

HotTag

Archive