Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Top n from each parentID ?

Posted on 2009-05-15
5
361 Views
Last Modified: 2012-05-07
Hi,

How can i select top n rows for each parentID, in MS SQL 2005 database
( this is very big table so i cant use functions, recursion etc, i think subquery will slow it down too)

Example table

parentID    someID   someOtherColums
1                   5
1                   6
1                   7
2                   8
2                   11
2                   23
3                   25
3                   20
4                   1
5                   3
5                   4
5                   31

now i need to select no more than 2 rows for each parentID, i will write my own logic wich 2 (first, last, some other filter, but lets asume i need first 2)

so results would look like:

1                   5
1                   6
2                   8
2                   11
3                   25
3                   20
4                   1
5                   3
5                   4


tnx
0
Comment
Question by:PapaStrumpf
5 Comments
 
LVL 57

Accepted Solution

by:
Raja Jegan R earned 350 total points
ID: 24393398
This should help you out:
SELECT parentID,someID, someOtherColums
FROM (
SELECT parentID,someID, someOtherColums, ROW_NUMBER() OVER ( partition BY parentID ORDER BY someID) rnum
FROM urtable) temp
WHERE rnum <=2 

Open in new window

0
 
LVL 1

Expert Comment

by:valerka
ID: 24393414
You have to use cursors with counters. Fill the temporary table with cursor and select from it.
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24393499
>>You have to use cursors with counters. Fill the temporary table with cursor and select from it.<<

Author can't even use function, recursion and you are talking about CURSOR :)
0
 
LVL 31

Assisted Solution

by:RiteshShah
RiteshShah earned 150 total points
ID: 24393546
if you want all records with tie than you can use, there is not much difference in the solution given by rrjegan17 and mine. there is just a little change.


SELECT parentID,someID, someOtherColums
FROM (
SELECT parentID,someID, someOtherColums, RANK() OVER ( partition BY parentID ORDER BY someID) rnum
FROM urtable) temp
WHERE rnum <=2 

Open in new window

0
 

Author Closing Comment

by:PapaStrumpf
ID: 31581835
Tnx for help.
I split some points to RiteshShah becouse actually i will need that Rank thing, and i didnt know it exists.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question