Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8836025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:23:51+00:00 2026-06-14T09:23:51+00:00

I have a stored procedure that does paging for the front end and is

  • 0

I have a stored procedure that does paging for the front end and is working fine. I now need to modify that procedure to group by four columns of the 20 returned and then only return the row within each group that contains the lowest priority. So when resort_id, bedrooms, kitchen and checkin (date) all match then only return the row that has the min priority. I have to still maintain the paging functionality. The @startIndex and @upperbound are parms passed into the procedure from the front end for paging. I’m thinking that RANK OVER (PARTITION BY) is the answer I just can’t quite figure out how to put it all together.

SELECT I.id,
       I.resort_id,
       I.[bedrooms],
       I.[kitchen],
       I.[checkin],
       I.[priority],
       I.col_1,
       I.col_2 /* ..... (more cols) */
FROM  ( 
        SELECT  ROW_NUMBER() OVER(ORDER by checkin) AS rowNumber, 
        *
        FROM Inventory
      ) AS I  
WHERE  rowNumber >=  @startIndex
AND    rowNumber <   @upperBound
ORDER BY rowNumber

Example 2 after fix:

SELECT I.resort_id,
       I.[bedrooms],
       I.[kitchen],
       I.[checkin],
       I.[priority],
       I.col_1,
       I.col_2 /* ..... (more cols) */
FROM Inventory i 
JOIN 
( 
    SELECT ROW_NUMBER() OVER(ORDER BY h.checkin) as rowNumber, MIN(h.id) as id
    FROM Inventory h
    JOIN (
        SELECT  resort_id, bedrooms, kitchen, checkin, id, MIN(priority) as priority
        FROM Inventory
        GROUP BY resort_id, bedrooms, kitchen, checkin, id
    ) h2 on h.resort_id = h2.resort_id and 
            h.bedrooms = h2.bedrooms and 
            h.kitchen = h2.kitchen and 
            h.checkin = h2.checkin and 
            h.priority = h2.priority
    GROUP BY h.resort_id, h.bedrooms, h.kitchen, h.checkin, h.priority
) AS I2 
    on i.id = i2.id
WHERE  rowNumber >=  @startIndex
AND    rowNumber <   @upperBound
ORDER BY rowNumber
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:23:53+00:00Added an answer on June 14, 2026 at 9:23 am

    I would accompish it this way.

    SELECT I.resort_id,
           I.[bedrooms],
           I.[kitchen],
           I.[checkin],
           I.[priority],
           I.col_1,
           I.col_2 /* ..... (more cols) */
    FROM Inventory i 
    JOIN 
    ( 
        SELECT ROW_NUMBER(ORDER BY Checkin) as rowNumber, MIN(id) id
        FROM Inventory h
        JOIN (
            SELECT  resort_id, bedrooms, kitchen, checkin id, MIN(priority) as priority
            FROM Inventory
            GROUP BY resort_id, bedrooms, kitchen, checkin
        ) h2 on h.resort_id = h2.resort and 
                h.bedrooms = h2.bedrooms and 
                h.kitchen = h2.kitchen and 
                h.checkin = h2.checkin and 
                h.priority = h2.priority
        GROUP BY h.resort_id, h.bedrooms, h.kitchen, h.checkin, h.priority
    ) AS I2 
        on i.id = i2.id
    WHERE  rowNumber >=  @startIndex
    AND    rowNumber <   @upperBound
    ORDER BY rowNumber
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a stored procedure that does, among other stuff, some inserts in different
I have a stored procedure which does sorting pnd Paging like the Sorting Custom
I have a GridView that use a stored procedure for custom paging and sorting
I have a stored procedure that does some parameter validation and should fail and
I have a stored procedure that does both the insert and the update in
I have a stored procedure that does this: SELECT TOP 4 FROM dbo.test (table
I have a stored procedure that does something like: IF @Param = '1' SELECT
I have a stored procedure that does a select and returns the results. I
So I have a stored procedure that does the following (modified out the data
I have a Stored Procedure that takes parameters, but does not return anything. How

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.