Wednesday, June 20, 2012

Another way of pagination in sql server


Today, I wrote a paginating query using ranking function row_number(), with help of my team mate Neha. Initial idea was from her, and later I hijacked it to post it here. :). Poor Neha..
Ceiling function seems to be crux of it.  First 3 columns are data columns, rest are informational for paging data.


select
          *
from
(
          SELECT                                                                         
          ID                                                      --Data Columns
          ,GrandSlamLocation                          --Data Columns    
          ,GrandSlamLogin                               --Data Columns
          ,ROW_NUMBER() OVER(ORDER BY ID) [S.No]                                             
          , ceiling(ROW_NUMBER() OVER(ORDER BY ID)/ 10.00)  as Page
          ,count(*) over() as TotalRecords
          ,ceiling(count(*) over()/10.00) as TotalPages
          from
          (
                   --Base Query Start
                   select
                             ID,                                  --Data Columns
                             GrandSlamLocation,      --Data Columns    
                             GrandSlamLogin            --Data Columns
                   from  
                             Ticket
                   --Base Query End
          )A
           
)B where Page =  1

No comments: