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 2000 Forums
 Transact-SQL (2000)
 Need multi top number of records

Author  Topic 

kpreston
Starting Member

2 Posts

Posted - 2004-10-28 : 12:29:09
I am trying to rewrite a select statement using the TOP clause for SQL. Right now we return a bulk set of data and then use VFP 8.0 to take out the necessary data. I was able to get the TOP to work (sort of). I need to be able to get the top 5 records for products purchased on, purchased off, and purchased froma different contract (three types). In a nutshell I need the top x records for type 1,2,and 3 (15 total records) and when I use the TOP clause I get only 5 total records. Any ideas?

Thanks in advance
Kelly

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2004-10-28 : 12:33:50
use union. number and type of coulmns must be same for all select used in union.

select *
from
(
select top 5 ... -- purchased on
from Table1
union all
select top 5 ... -- purchased off
from Table1
union all
select top 5 ... -- purchased from
from Table1
) t1


Go with the flow & have fun! Else fight the flow
Go to Top of Page

kpreston
Starting Member

2 Posts

Posted - 2004-10-28 : 13:49:44
When I tried the union piece I am getting a different total of units sold when I run the whole union clause versus just running one branch of the select statement. And the order is not showing the correct order with the union piece of the statment. Any ideas?

kelly
Go to Top of Page
   

- Advertisement -