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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:07:29+00:00 2026-06-01T23:07:29+00:00

I have a case where I need to show only the top rows based

  • 0

I have a case where I need to show only the top rows based on a setting in a table and the ordinal set.

Example dataset below shows two customers; each of the customers have a different product.
Since NumRowsToShow is “1” I only want to show one row (the top row based on ordinal) for EACH Customer.

| CustomerID | ProductID | Ordinal | NumRowsToShow |
+------------+-----------+---------+---------------+
| 1          |A          |1        |1              |
| 1          |B          |2        |1              |
| 1          |C          |3        |1              |
| 5          |D          |1        |1              |
| 5          |E          |2        |1              |
| 5          |F          |3        |1              |

The result set after query is run should be

| CustomerID | ProductID |
+------------+-----------+
| 1          |A          |
| 5          |D          |

In the same scenario if NumRowsToShow were 1 for customerID 1 and 2 for CustomerID 5 I would see something like.

| CustomerID | ProductID | Ordinal | NumRowsToShow |
+------------+-----------+---------+---------------+
| 1          |A          |1        |1              |
| 1          |B          |2        |1              |
| 1          |C          |3        |1              |
| 5          |D          |1        |2              |
| 5          |E          |2        |2              |
| 5          |F          |3        |2              |

The result set after query is run should be

| CustomerID | ProductID |
+------------+-----------+
| 1          |A          |
| 5          |D          |
| 5          |E          |

How can this be done?

Including a screen cap of actual result set with highlights of what I’m trying to filter down to which may be a little helpful.

Screencap
(source: harpernet.net)

  • 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-01T23:07:30+00:00Added an answer on June 1, 2026 at 11:07 pm

    It feels like “cheating in the exams”:

    SELECT CustomerID, ProductID
    FROM tableX
    WHERE Ordinal <= NumRowsToShow
    

    If, as comments suggest, the Ordinal can have 10, 20, 30 values and not only 1, ..., n values, then this will work:

    SELECT t.CustomerID, t.ProductID
    FROM tableX AS t
      JOIN tableX AS tt
        ON  tt.CustomerID = t.CustomerID
        AND tt.Ordinal <= t.Ordinal
    GROUP BY t.CustomerID
           , t.ProductID
           , t.NumRowsToShow
    HAVING COUNT(*) <= t.NumRowsToShow
    

    or even better, the:

    SELECT CustomerID, ProductID
    FROM
      ( SELECT CustomerID, ProductID, NumRowsToShow
             , ROW_NUMBER() OVER( PARTITION BY CustomerID 
                                  ORDER BY Ordinal
                                ) AS Rn
        FROM tableX
      ) AS tmp
    WHERE Rn <= NumRowsToShow ;
    

    Test in: SQL-Fiddle


    Your table looks to be not normalized. The NumRowsToShow columns has duplicate infomation and that can lead to update anomalies. This:

    | CustomerID | ProductID | Ordinal | NumRowsToShow |
    +------------+-----------+---------+---------------+
    | 1          |A          |1        |1              |
    | 1          |B          |2        |1              |
    | 1          |C          |3        |1              |
    | 5          |D          |1        |2              |
    | 5          |E          |2        |2              |
    | 5          |F          |3        |2              |
    

    could be normalized to 2 tables:

    | CustomerID | ProductID | Ordinal |
    +------------+-----------+---------+
    | 1          |A          |1        |
    | 1          |B          |2        |
    | 1          |C          |3        |
    | 5          |D          |1        |
    | 5          |E          |2        |
    | 5          |F          |3        |
    

    and:

    | CustomerID | NumRowsToShow |
    +------------+---------------+
    | 1          |1              |
    | 5          |2              |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a case where I need to update a jqgrid based on some
I have a use case where I only need to store certain fields to
Have a use case wherein need to maintain a connection open to a database
I have a case where I need to select a random item, but I
I have a case where I need to load bitmap from a resource dll
I have a business case whereby I need to be able to specify my
I have a use case where I need to call a (non-static) method in
I have a CakePHP 1.2 application. I'm running into the case where I need
I have a complicated problem, and I need help. I have a base case,
I have a strong use case for pre-allocating all the memory I need upfront

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.