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.