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

  • Home
  • SEARCH
  • 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 152269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:35:35+00:00 2026-05-11T09:35:35+00:00

Lets say I have a table containing several hundred million rows that looks something

  • 0

Lets say I have a table containing several hundred million rows that looks something like this:

memID | foo  | bar  | foobar 1     | blah | blah | blah 1     | blah | blah | blah 1     | blah | blah | blah 1     | blah | blah | blah 1     | blah | blah | blah 1     | blah | blah | blah 1     | blah | blah | blah 2     | blah | blah | blah 2     | blah | blah | blah 2     | blah | blah | blah 2     | blah | blah | blah 2     | blah | blah | blah . . . 10001 | blah | blah | blah 10001 | blah | blah | blah 

I need a query that will return the top N rows for each memID in a range of member IDs. For example, if N = 3 and the range is 0-2 it should return

memID | foo  | bar  | foobar 1     | blah | blah | blah 1     | blah | blah | blah 1     | blah | blah | blah 2     | blah | blah | blah 2     | blah | blah | blah 2     | blah | blah | blah   

I’ve considered a couple approaches, first creating a massive

SELECT TOP (3) * FROM table WHERE memID = 0 UNION ALL SELECT TOP (3) * FROM table WHERE memID = 1 . . . 

query in code. This isn’t really a realistic option for probably obvious reasons.

The second approach is to create a temporary table and loop over the range of memIDs inserting the TOP 3 for each memID into that temporary table.

WHILE @MemID < 10000 AND @MemID > 0   BEGIN     INSERT INTO tmp_Table     SELECT TOP (3) *      FROM table      WHERE memID = @MemID      SET @MemID = @MemID + 1     END 

This works, but I’m wondering if there is a more elegant single query solution that I’m missing.

Cadaeic gave me an answer that works without tinkering, but thank you to everyone that suggested analytics, it looks like I have some serious reading to do.

  • 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. 2026-05-11T09:35:35+00:00Added an answer on May 11, 2026 at 9:35 am
    declare @startID int, @endID int, @rowsEach int select @startID = 0, @endID = 2, @rowsEach = 3   select * from (     select memID, foo, bar, foobar, row_number() over (partition by dense_rank order by dense_rank) [rank_row]     from     (         select memID, foo, bar, foobar, dense_rank() over (order by memID) [dense_rank]         from #test         where memID between @startID and @endID     ) a ) b where rank_row <= @rowsEach 

    The result:

    memID       foo  bar  foobar rank_row ----------- ---- ---- ------ -------------------- 1           blah blah blah   1 1           blah blah blah   2 1           blah blah blah   3 2           blah blah blah   1 2           blah blah blah   2 2           blah blah blah   3 

    And here’s the set-up code if you’d like to test locally:

    create table #test (       memID     int not null     , foo       char(4) not null     , bar       char(4) not null     , foobar    char(4) not null )  insert into #test (memID, foo, bar, foobar) select 1, 'blah', 'blah', 'blah' union all select 1, 'blah', 'blah', 'blah' union all select 1, 'blah', 'blah', 'blah' union all select 1, 'blah', 'blah', 'blah' union all select 1, 'blah', 'blah', 'blah' union all select 1, 'blah', 'blah', 'blah' union all select 1, 'blah', 'blah', 'blah' union all select 2, 'blah', 'blah', 'blah' union all select 2, 'blah', 'blah', 'blah' union all select 2, 'blah', 'blah', 'blah' union all select 2, 'blah', 'blah', 'blah' union all select 10001, 'blah', 'blah', 'blah' union all select 10001, 'blah', 'blah', 'blah' union all select 10001, 'blah', 'blah', 'blah' 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 64k
  • Answers 64k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer If I understand correctly, you think the two values are… May 11, 2026 at 10:55 am
  • added an answer The compiler doesn't know it's an array, it's trusting the… May 11, 2026 at 10:55 am
  • added an answer In your application.xml file, set resizable to false under initialWindow:… May 11, 2026 at 10:55 am

Related Questions

Lets say I have a table in a sql server 2000 database called TransactionType:
Let's say I have a table that represents a super class, students . And
Let's say I have a table like this: name | score_a | score_b -----+---------+--------
Let's say I have a table tbl with columns id and title . I
In MS Transact SQL, let's say I have a table (Orders) like this: Order
Let's say I have a payments table like so: CREATE TABLE Payments ( PaymentID
Let's say that I want to have a table that logs the date and
Let's say I have two tables: Report Comment And assuming I have a database
Lets say I have a Dictionary object: Dictionary myDictionary<int, SomeObject> = new Dictionary<string, SomeObject>();
Lets say I have a single object of type Car which I want to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.