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

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])

What is your SQL Server version?
I think there is no way to get the count except for using temp table, e.g.
select *
INTO #TempResults
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 )
select @TotalCount = TotalRows from #TempResult
SELECT .. FROM #TempResult
ORDER BY CoordinatorId
See also this very recent article about the subject you ask:
Paging
a Query with SQL Server
For every expert, there is an equal and opposite expert. - Becker's Law
My blog
My TechNet articles

Similar Messages

  • I have used the "description" spot in iPhoto for in-depth additional information for photos of my ancestors. Now I am wanting to know if there is a way to print the photos along with the descriptions; either photo by photo, or maybe more than one per page

    I have used the "description" spot in iPhoto for in-depth additional information for photos of my ancestors. Now I am wanting to know if there is a way to print the photos along with the descriptions; either photo by photo, or maybe more than one per page, or possibly in some sort of booklet format. Thank you very much.

    You can make a Book and have a photo on one side and your text description on the other.

  • Most efficient way to extract the amplitude of a signal and display on an intensity graph

    Hi All,
    I am having difficulty to display the amplitude of an voltage signal (voltage picked up by NI DAQ card).
    what I am trying: Voltage signal (Dynamic data type)--> 2D array of dynamic data type (using 2 nested for loops) --> normal 2D array with scalar values --> intensity graph
    I have difficulty to convert the dynamic 2D array to scalar 2D arry.
    I only want to extract the amplitude of the imcoming signal.. any body can give some advice? thanks in advance..
    Charlie

    Hi Hillary,
    Thanks for your reply.
    Yes, I did tried to use Amplitude and Levels Express vi, it should be the one I am looking for, but I found it not work in my case, I need to put the value of amplitudes into a 2D array.
    If I use Amplitude and Levels Express vi, it will give me a 3D array, I have found the solution, just convert the dynamic data to single scalar. (see pic attached)
    Anybody can help me to solve the following 2 problems:
    1. The speed of this program is too low, i think ii due to the DAQ assistant in the 2 for loops (for creating 2D array), it should be able to re-write using DAQmx (as "centerbolt" told me), I am working on this now, if anybody can help, I will appreciate that.
    2. Is there any other way to creat the 2D array (for purpose of increasing speed also).
    Thanks,
    Charlie

  • Most efficient way to insert into a story with many floating images?

    I have a document with many floating images. They must all be floating because otherwise I do not get the Caption numbering right and it is impossible (because some images take an entire page) to use only anchored images.
    Now, I have to insert a large part into the middle / at the beginning of a section. If I do that the text will flow, but the images remain in place. This is extremely slow working because of all teh movements I have to do on all the images. What I would like to do is to let the pages after the page where I am entering remain the same and when I insert text and images this should just move up one page at a time. Then, at the end, I can do the fine tuning of attaching both parts again. Is there a way to do that in an efficient way?
    What I now often do is move all the floating images out of the pages, then insert the new stuff, move the images back, then make sure all the references are fine (e.g. if they refer to an image on another page, the style becomes paragraph number + page numer). For some sections this is a hideous amount of work (many, many images).
    Is there a smarter way. I have been thinking about splitting and later rejoining a section. If I add pages to one section, the following sections are not damaged, after all.
    What is the best way to do this?
    Thanks in advance.

    I have a document with many floating images. They must all be floating because otherwise I do not get the Caption numbering right and it is impossible (because some images take an entire page) to use only anchored images.
    Now, I have to insert a large part into the middle / at the beginning of a section. If I do that the text will flow, but the images remain in place. This is extremely slow working because of all teh movements I have to do on all the images. What I would like to do is to let the pages after the page where I am entering remain the same and when I insert text and images this should just move up one page at a time. Then, at the end, I can do the fine tuning of attaching both parts again. Is there a way to do that in an efficient way?
    What I now often do is move all the floating images out of the pages, then insert the new stuff, move the images back, then make sure all the references are fine (e.g. if they refer to an image on another page, the style becomes paragraph number + page numer). For some sections this is a hideous amount of work (many, many images).
    Is there a smarter way. I have been thinking about splitting and later rejoining a section. If I add pages to one section, the following sections are not damaged, after all.
    What is the best way to do this?
    Thanks in advance.

  • Sending an Email from the app along with table data

    Hi All,
    I would like to send an email from my app. I tried to put in the field attachment SQL this query: select * from Table_Name where id = (select max(id) from Table_Name) but it did not work. It showed me this error:
    1 error has occurred
    Wrong number of columns selected in the SQL query. See Help of attribute for details.
    I would like to send an email every time the user insert a row with that row in the email... How can I do that?
    Thank You.

    What I would suggest is the following:
    Build an After Insert Trigger on the table in question. In this trigger build a string with ALL the columns that are being inserted into the table. Then you will need to build some local variable to submit your mail. You will send your e-mail via UTIL_SMTP mail, since you may NOT always being in an APEX application when the table is updated..
    Ask away if you have more questions..
    Thank you,
    Tony Miller
    Webster, TX
    I am a Ranger! We walk in the dark places no others will enter! We stand on the bridge and no one may pass! We live for the One, we die for the One!
    If this question is answered, please mark the thread as closed and assign points where earned..

  • What is the most efficient way of passing large amounts of data through several subVIs?

    I am acquiring data at a rate of once every 30mS. This data is sorted into clusters with relevant information being grouped together. These clusters are then added to a queue. I have a cluster of queue references to keep track of all the queues. I pass this cluster around to the various sub VIs where I dequeue the data. Is this the most efficient way of moving the data around? I could also use "Obtain Queue" and the queue name to create the reference whenever I need it.
    Or would it be more efficient to create one large cluster which I pass around? Then I can use unbundle by index to pick off the values I need. This large cluster can have all the values individually or it co
    uld be composed of the previously mentioned clusters (ie. a large cluster of clusters).

    > I am acquiring data at a rate of once every 30mS. This data is sorted
    > into clusters with relevant information being grouped together. These
    > clusters are then added to a queue. I have a cluster of queue
    > references to keep track of all the queues. I pass this cluster
    > around to the various sub VIs where I dequeue the data. Is this the
    > most efficient way of moving the data around? I could also use
    > "Obtain Queue" and the queue name to create the reference whenever I
    > need it.
    > Or would it be more efficient to create one large cluster which I pass
    > around? Then I can use unbundle by index to pick off the values I
    > need. This large cluster can have all the values individually or it
    > could be composed of the previously mentioned clusters (i
    e. a large
    > cluster of clusters).
    It sounds pretty good the way you have it. In general, you want to sort
    these into groups that make sense to you. Then if there is a
    performance problem, you can arrange them so that it is a bit better for
    the computer, but lets face it, our performance counts too. Anyway,
    this generally means a smallish number of groups with a reasonable
    number of references or objects in them. If you need to group them into
    one to pass somewhere, bundle the clusters together and unbundle them on
    the other side to minimize the connectors needed. Since the references
    are four bytes, you don't need to worry about the performance of moving
    these around anyway.
    Greg McKaskle

  • I am giving my old MacBook Air to my granddaughter.  What is the most efficient way to erase all the data on it?

    I am giving my old MacBook Air to my granddaughter.  What is the most efficient way to erase the data?

    You have two options.....
    One is to do a clean reinstall of your OS - if you still have the USB installer that came with your Macbook Air...
    The second option is to create a new user (your granddaugher's name).....Deauthorize your Macbook Air from your Itunes and Appstore.....
    Restart your Macbook after you've created your granddaughter's user name, login under your granddaughter's username and delete your username.
    Search your Macbook for your old files and delete them.....
    Good luck...

  • Most efficient way to get document names?

    I was wondering what is the most efficient way to get the document names in a container? Use the built in 'name' index somehow, or is there an 'efficient' XPath/XQuery?
    We've been using the XPath /* which is fine with small instances, but causes a java heap out of member error on large XML instances i.e. /* gets everything which is not ideal when all we want are document names.
    Thx in advance,
    Ant

    Hi Antony,
    Here is an example for retrieving the document names on c++:
    void doQuery(XmlContainer &container,
    XmlQueryContext &context,
    const std::string &XPath)
    XmlResults results(container.queryWithXPath(0, XPath, &context));
    // Iterate through the result set as is normal
    XmlDocument theDocument;
    while(results.next(theDocument))
    std::cout << "Found document named: "
    << theDocument.getName() << std::endl;
    Regards,
    Bogdan Coman

  • [11g] most efficient way to calculate size of xmltype type column

    I need to check the current size of some xmltype column in a BIU trigger.
    I don't think it's good to use
      length(:new.xml_data.GetStringVal());
    or
      dbms_lob.GetLength(:new.xml_data.GetClobVal());
    What's the most efficient way to get the storage size?
    I don't need the string serialized size.
    It could also be the internal storage size (incl. administration data overhead).
    - thanks!
    regards,
    Frank

    > May I ask for what reason you need to know it?
    I need to handle very large XML document output, which currently hits the internal xmltype limitation of 4GByte, when aggregating XML document fragments for this.
    > You'll get a relevant answer if you give us relevant information :
    > - exact db version
    SELECT * FROM PRODUCT_COMPONENT_VERSION;
    product
    version
    status
    1
    NLSRTL
    11.2.0.3.0
    Production
    2
    Oracle Database 11g Enterprise Edition
    11.2.0.3.0
    64bit Production
    3
    PL/SQL
    11.2.0.3.0
    Production
    4
    TNS for Linux:
    11.2.0.3.0
    Production
    > - DDL of your table
    > XML stored as XMLType datatype can use different storage models, depending on the version.
    I don't use dedicated storage clause.
    But i am hitting the problem already for aggregation into some xmltype variable in PL/SQL
    - BEFORE writing back to a result table.
    Can i avoid such problems, when writing to a table DIRECTLY w/o intermediate xmltype PL/SQL variable
    - depending on the storage clause?
    The reason why asking how to get the size of some xmltype (in table column and/or in PL/SQL variable) is, that i am thinking of a threshold detection.
    In case threshold is reached: outsource XML fragment so far to some separate CLOB storage, and insert a smaller meta-information reference representing that in the output document.
    Finally leave up to the client system to use <xs:include> (or alike) to construct the complete document.
    rgds,
    Frank

  • The most efficient way to search a large String

    Hi All,
    2 Quick Questions
    QUESTION 1:
    I have about 50 String keywords -- I would like to use to search a big String object (between 300-3000 characters)
    Is the most efficient way to search it for my keywords like this ?
    if(myBigString.indexOf("string1")!=1 || myBigString.indexOf("string2")!=1 || myBigString.indexOf("string1")!=1 and so on for 50 strings.)
    System.out.println("it was found");
    QUESTION 2:
    Can someone help me out with a regular expression search of phone number in the format NNN-NNN-NNNN
    I would like it to return all instances of that pattern found on the page .
    I have done regular expressions, in javascript in vbscript but I have never done regular expressions in java.
    Thanks

    Answer 2:
    If you have the option of using Java 1.4, have a look at the new regular expressions library... whose package name I forget :-/ There have been articles published on it, both at JavaWorld and IBM's developerWorks.
    If you can't use Java 1.4, have a look at the jakarta regular expression projects, of which I think there are two (ORO and Perl-like, off the top of my head)
    http://jakarta.apache.org/
    Answer 1:
    If you have n search terms, and are searching through a string of length l (the haystack, as in looking for a needle in a haystack), then searching for each term in turn will take time O(n*l). In particular, it will take longer the more terms you add (in a linear fashion, assuming the haystack stays the same length)
    If this is sufficient, then do it! The simplest solution is (almost) always the easiest to maintain.
    An alternative is to create a finite state machine that defines the search terms (Or multiple parallel finite state machines would probably be easier). You can then loop over the haystack string a single time to find every search term at once. Such an algorithm will take O(n*k) time to construct the finite state information (given an average search term length of k), and then O(l) for the search. For a large number of search terms, or a very large search string, this method will be faster than the naive method.
    One example of a state-search for strings is the Boyer-Moore algorithm.
    http://www-igm.univ-mlv.fr/~lecroq/string/tunedbm.html
    Regards, and have fun,
    -Troy

  • What is the best, most efficient way to read a .xls File and create a pipe-delimited .csv File?

    What is the best and most efficient way to read a .xls File and create a pipe-delimited .csv File?
    Thanks in advance for your review and am hopeful for a reply.
    ITBobbyP85

    You should have no trouble doing this in SSIS. Simply add a data flow with connection managers to an existing .xls file (excel connection manager) and a new .csv file (flat file). Add a source to the xls and destination to the csv, and set the destination
    csv parameter "delay validation" to true. Use an expression to define the name of the new .csv file.
    In the flat file connection manager, set the column delimiter to the pipe character.

  • What's the most efficient way to serve a file from a servlet?

    I have a servlet that does various different things depending on the needs. Sometimes it dynamically generates content, and sometimes all it does is send a file out, with no alteration.
    What is the most efficient way to just send a file?
    One option:
    OutputStream os = response.getOutputStream();
    InputStream is = new FileInputStream(...)
    (send all the bytes from is to os, the regular way using a buffer)Another option is to say:
    RequestDispatcher rd = response.getRequestDispatcher(fileName);
    rd.forward();Any other options? What's the prefered way of doing this?
    I know the rule of "don't optimize too early" but this is a situation where we need to get the maximum amount of files served with the hardware we have, and it's going to be a lot of static files, so efficiency is important.
    Thanks

    Ok, that's what I thought. It would be nice if there were a "response.sendStream(InputStream input)" method in the ServletResponse class. Even nicer would be a sendFile or sendChannel or something. This is probably a common usage and it's a place where the container has many opportunities for optimization. For example, it could call the operating systems send_file kernel call so the entire transfer would be done directly from the disk controller to the ether card (on systems that support that).
    For now I'll just do my own buffered copy.

  • What is the most efficient way to compare two Lists?

    List A{itemId,itemName} [1,xyz] [9,iyk] [4,iuo] .......
    List B{itemId,item price} [2,999] [9,888] [1, 444].......
    Assume A will be a much larger list than B
    I am trying to find all the items with same itemiId. what would be the most efficient way to do that?
    Thanks!

    Tinkerbell. wrote:
    BigDaddyLoveHandles wrote:
    You wrote:
    Can we assume that an itemId only occurs once in each list? You're the one making claims and assumptions, not me.No in #4 I asked the OP to verify an assumption.An assumption that couldn't possibly be true. Why are you wasting our time?

  • What is the most efficient way to have full access to the front panel on RT Labview?

    I have a RT machine that needs to do its job and also port the front panel to an external machine over the network. What is the most efficient way to do it? Using as little of the RT time as possible but providing full functionality to the RT front panel.
    So far I have been using it directly from Labview - running the VI on a remote (RT) and have the front panel on local Labview (WINDOWS). I know I can do it with also through WWW (not very happy with that though).
    LV2009 SP1.
    Thanks

    Running a compiled executable on the RT target, rather than running it within the development environment, is probably slightly more efficient but limits you to the web interface.  If you're running within the LabVIEW environment, I doubt there's a noticeable difference in efficiency from the RT perspective between the web server and the LabVIEW front panel, although that's mostly a guess (I would expect the RT system to send identical data in each case, once the front panel is loaded).  Those are your only options in modern LabVIEW versions.  In LabVIEW 7.1 you could build an executable that acted as the front panel for an RT system, but that feature does not exist in recent versions.  However, a quick search turned up this document with code to approximately duplicate that behavior, perhaps it will work for you?

  • What is the most efficient way to convert a static site to a responsive site using Dreamweaver?

    I need to convert an old site made in Dreamweaver to be responsive to any monitor size. What is the most efficient way to do this?

    Depending on what you have to work with and how it was coded, it might be doable and then again not.  Suffice it to say, there are no magic buttons that will do this for you. Also consider that mobile & tablet users interact differently with their web devices. So your navigation & forms must be finger friendly.  Also images & content must make mobile users happy without killing their dataplans.  There's a lot of planning that goes into making a good Responsive Web site.
    Nancy O.

Maybe you are looking for