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 8519223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:11:26+00:00 2026-06-11T06:11:26+00:00

I am trying to select 5 records in each ItemCategoryID with paging, with the

  • 0

I am trying to select 5 records in each ItemCategoryID with paging, with the query below I am able to get 5 records in each category but paging is not working, as i have declared page size 10, but i am getting 19 records and @ItemCounter TotalCount is coming as 45… i m not getting how to solve it..here is the query:

DECLARE @PageIndex int = 1
DECLARE @PageSize int = 10
DECLARE @StartRow int
DECLARE @EndRow int
SET @StartRow = (@PageSize * (@PageIndex - 1))  + 1  
SET @EndRow = @PageSize * @PageIndex + 1
DECLARE @ItemCounter int
SELECT @ItemCounter = Count(*)FROM dbo.Auctions WHERE AuctionStatus=1;
WITH Auctions AS
    (
        SELECT  ROW_NUMBER() OVER 
        (PARTITION BY ItemCategoryID  ORDER BY AuctionID) AS RowNumber,                 
            AuctionID, 
            ItemCategoryID  ,                             
            @ItemCounter TotalCount                         
            FROM Auctions                 
            WHERE                 
            AuctionStatus=1 
     )

    SELECT a.* FROM Auctions a  
    WHERE  a.RowNumber <=3 AND a.RowNumber 
    BETWEEN @StartRow AND @EndRow - 1

Thanks in advance.

  • 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-11T06:11:27+00:00Added an answer on June 11, 2026 at 6:11 am
    a.RowNumber <=3 AND a.RowNumber BETWEEN @StartRow AND @EndRow - 1
    

    What exactly do you expect from this? the row number has to be between start and end AND it has to be less than or equal to 3? What is start is 4? Any End above 3 is irrelevant.

    I’m going to make a leap of faith here and say that the correctly stated requirement for your problem is:

    • for each item category, return at most 4 auctions where status = 1
    • page the result

    so you obviously have two counters to consider, one for auctions within category and one for paging:

    WITH Auctions AS
    (
        SELECT  ROW_NUMBER() OVER 
        (PARTITION BY ItemCategoryID  ORDER BY AuctionID) AS AuctionNumber,                 
            AuctionID, 
            ItemCategoryID                             
            FROM Auctions                 
            WHERE                 
            AuctionStatus=1 
     ),
    Paging as (
      SELECT ROW_NUMBER() OVER (ORDER BY ItemCategoryID, AuctionID) as RowNumber
       , a.* 
      FROM Auctions a  
      WHERE AuctionNumber <= 3 
    )
    SELECT * 
      FROM Paging 
      WHERE  RowNumber BETWEEN @StartRow AND @EndRow - 1;
    

    Performance will be likely bad, but nobody can design any performance w/o data schema knowledge (table definition, indexes etc) which are missing from the problem statement.

    TotalRecordCount:

    WITH Auctions AS
    (
      SELECT  ROW_NUMBER() OVER 
        (PARTITION BY ItemCategoryID  ORDER BY AuctionID) AS AuctionNumber,                 
        AuctionID, 
        ItemCategoryID  ,                             
      FROM Auctions                 
      WHERE AuctionStatus=1 
    )
    SELECT @total = COUNT(*) 
     FROM Auctions a  
     WHERE AuctionNumber <= 3;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to formulate the right query to select records that each have a
I am trying to get records of each category limited to 5, but I
I am trying to select records that have the curent month using: SELECT *
I have an issue with psql. I am trying to select the records from
I need some help with SQL Query. I am trying to select all records
I am trying to sort the each columns in my JQGrid but its not
I am trying to select multiple columns, but not all of the columns, from
I am trying to select all the records from a sqlite db I have
I am trying to write a query to get count of employees in each
I'm trying to select a record with the most effective votes. Each record has

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.