Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Most efficient way to return the totalcount along with pagewise data [via pagenumber/pagesize as input]

$
0
0

I have following query to return the page data

declare @pageSize    INT = Null
        ,@pageNumber INT = Null
declare @totalcount int 
set @pageSize =15
set @pageNumber = 1

select *
from   (SELECT ROW_NUMBER()
                 OVER(
                   ORDER BY dbo.coordinator_event.CoordinatorId) AS RowNumberForPaging
               ,dbo.coordinator_event.EventId
               ,dbo.coordinator_event.NumberOfParticipantAllowed
               ,dbo.coordinator_event.RegistrationClosingDate
               ,dbo.coordinator_event.CoordinatorId
               ,dbo.royalevents_royalevent.title_en              AS EventTitleEnglish
               ,dbo.royalevents_royalevent.StartDateTime
               ,Count(dbo.event_attendee_registration.FullName)  ParticipantRegistration             
               , COUNT(*) OVER(PARTITION BY 1) as TotalRows
        FROM   dbo.coordinator_event
               inner JOIN dbo.royalevents_royalevent
                       ON dbo.coordinator_event.EventId = dbo.royalevents_royalevent.base_id
               left JOIN dbo.event_attendee_registration
                      ON dbo.coordinator_event.CoordinatorId = dbo.event_attendee_registration.CoordinatorId
                         and royalevents_royalevent.base_id = event_attendee_registration.EventId
        where  coordinator_event.CoordinatorId = 3
        group  by dbo.coordinator_event.EventId
                  ,dbo.coordinator_event.NumberOfParticipantAllowed
                  ,dbo.coordinator_event.RegistrationClosingDate
                  ,dbo.coordinator_event.CoordinatorId
                  ,dbo.royalevents_royalevent.title_en                  
                  ,dbo.royalevents_royalevent.StartDateTime                  
                  ) MyTable
WHERE  RowNumberForPaging >= ( ( ( @pageSize * @pageNumber ) - @pageSize ) + 1 )
       AND RowNumberForPaging <= ( @pageSize * @pageNumber )
ORDER  BY CoordinatorId 
I want's two thing with it. One thing is about how i can easily return @totalcount as separate parameter instead of part of the result set and secondly if there is a way i can optimize above row_number query to achieve paging


Kamran Shahid Application Developer (MCP,MCAD,MCSD.NET,MCTS,MCPD.net[web])


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>