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
 select top 15 and send top 5 , 5, 5

Author  Topic 

shanmugaraj
Posting Yak Master

219 Posts

Posted - 2006-07-24 : 08:05:17
I have a requirement to select top 15 from a table

select top 15 from students
============================
now , i want to send first 5 to Class A , Next 5 to Class B , Next 5 to Class C


how can i
select top 5 to 10 from top 15 from table students
==========================================

select top 10 to 15 from top 15 from table students
==========================================

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-07-24 : 08:12:24
See this examle
declare @table table
(
rank int,
student varchar(10)
)

insert into @table
select 1, 'A' union all
select 2, 'B' union all
select 3, 'C' union all
select 4, 'D' union all
select 5, 'E' union all
select 6, 'F'

select top 2 rank, student
from @table
order by rank

select top 2 rank, student
from
(
select top 4 rank, student
from @table
order by rank
) a
order by rank desc


select top 2 rank, student
from
(
select top 6 rank, student
from @table
order by rank
) a
order by rank desc



KH

Go to Top of Page

shanmugaraj
Posting Yak Master

219 Posts

Posted - 2006-07-24 : 08:14:16
Respected khtan ,
Thanks for the example
Kindly provide me few time to check the code
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-07-24 : 08:16:17
Or
select	rank,
student,
'Class ' + CHAR(65 + (rank - 1) / 2)
from @table



Peter Larsson
Helsingborg, Sweden
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-07-24 : 10:56:52
If it is for pagination, refer
http://weblogs.sqlteam.com/jeffs/archive/2003/12/22/672.aspx

Madhivanan

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

- Advertisement -