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
 General SQL Server Forums
 New to SQL Server Programming
 Qry Help

Author  Topic 

avmreddy17
Posting Yak Master

180 Posts

Posted - 2008-03-03 : 14:04:08
Can some one help me rewriting the Qry so that I get TOP 10 rows from the cNumbers table for each branchCodeExternal

SELECT * FROM ConfigDB..cNumbers
WHERE BranchCodeInternal IN ( SELECT BranchCodeInternal
FROM ConfigDB..CTSBranches
WHERE BranchCodeExternal IN (
'001',
'129',
'162'))
ORDER BY BranchCodeInternal

Thx

avmreddy17
Posting Yak Master

180 Posts

Posted - 2008-03-03 : 14:26:02
Thanks..I guess I got it... If there is more easy way of wring this..pls reply.. Thanks for any help


Select BranchCodeInternal , cNumberExternal
From ConfigDB..cNumbers as t1
Where (Select count(distinct cNumberExternal) from ConfigDB..cNumbers Where BranchCodeInternal = t1.BranchCodeInternal and cNumberExternal >= t1.cNumberExternal)<=10
AND BranchCodeInternal IN ( SELECT BranchCodeInternal
FROM ConfigDB..CTSBranches
WHERE BranchCodeExternal IN (
'001',
'129',
'162'))
Order By BranchCodeInternal, cNumberExternal
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-03-03 : 14:33:41
Are you using SQL 2005 or 2000? If you are using 2005, see this:

http://weblogs.sqlteam.com/jeffs/archive/2007/03/30/More-SQL-Server-2005-Solutions.aspx

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

avmreddy17
Posting Yak Master

180 Posts

Posted - 2008-03-03 : 14:46:12
using SQL Server 2000
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-03-03 : 15:36:07
Ah, a bit harder than. See: http://www.eggheadcafe.com/community/aspnet/9/10017146/select-the-top-n-rows-fro.aspx

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page
   

- Advertisement -