Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Select Top 2 based on a column

Author  Topic 

jerry8989
Starting Member

6 Posts

Posted - 2008-01-14 : 14:05:37
I have a table that has 5 columns that looks like this
Region RegionName Row
1 Test1 1
1 Test2 2
1 Test3 3
2 Test4 1
2 Test5 2
2 Test6 3
3 Test7 1
3 Test8 2
3 Test9 3
3 Test10 4


I need to select the top 2 by Region so I need my result set to be

Region RegionName Row
1 Test1 1
1 Test2 2
2 Test4 1
2 Test5 2
3 Test7 1
3 Test8 2


Thanks

rohitkumar
Constraint Violating Yak Guru

472 Posts

Posted - 2008-01-14 : 14:14:28
if you already have Row column is'nt it a simple

...where row <= 2
Go to Top of Page

jerry8989
Starting Member

6 Posts

Posted - 2008-01-14 : 14:19:42
I'm sorry I didn't display the data correctly.

There might be instances where a specific region only has one row but I still need 2 to come back. The Row column for the blank row is 7

1 Test1 1
1 Test2 2
1 7
2 Test4 1
2 Test5 2
2 7
2 8
2 9
3 Test7 1
3 Test8 2
3 Test9 3
3 Test10 4

Sorry if i'm not explaining it good enough
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-01-14 : 14:36:42
quote:
Originally posted by jerry8989

I'm sorry I didn't display the data correctly.

There might be instances where a specific region only has one row but I still need 2 to come back. The Row column for the blank row is 7

1 Test1 1
1 Test2 2
1 7
2 Test4 1
2 Test5 2
2 7
2 8
2 9
3 Test7 1
3 Test8 2
3 Test9 3
3 Test10 4

Sorry if i'm not explaining it good enough



>>Sorry if i'm not explaining it good enough
I'm afraid that is true

I thought I understood until I saw this: "The Row column for the blank row is 7" ??

Rather than have us continue to guess what you want, why don't you try asking again but this time follow the instructions in this link to "set up" your question:
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx



Be One with the Optimizer
TG
Go to Top of Page

rohitkumar
Constraint Violating Yak Guru

472 Posts

Posted - 2008-01-14 : 14:39:11
what do you mean when you say "a specific region only has one row but I still need 2 to come back"
Go to Top of Page

jerry8989
Starting Member

6 Posts

Posted - 2008-01-14 : 15:00:41
I figured it out.

Thanks for the help
Go to Top of Page

rohitkumar
Constraint Violating Yak Guru

472 Posts

Posted - 2008-01-14 : 16:11:53
you sure? or people here scared you?
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-01-16 : 02:33:33
http://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/multipurpose-row-number-function.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -