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

Related Questions

Lets say I have a table containing many many rows like this: ID Range
Lets Say i have a table like this WEB_LIST_TABLE KEY Value ---------------------------------------- 134 google.com
Lets say I have one table called REVIEWS This table has Reviews that customers
Lets say that I have a table containing users . Each row in that
Hi lets say I have a table like this: ________________________________________________________ | | id |
Let's say I have a simple stored procedure that looks like this (note: this
lets say i have a table like this : a | b | c
For examples sake, lets say I have a table containing these columns ID (primary
Let's say we have a [Valuations] table containing several values per date and per
I have a table (Oracle database) that looks like: CREATE TABLE example ( idEx

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.