It's been a long time since I wrote SQL in SQL Server (My company is
an Oracle shop) but I remember being able to use the TOP and BOTTOM
keywords to get a set of data.
So, if you wanted the 21 thru 40th record:
SELECT BOTTOM 20
*
FROM
(SELECT TOP 40
*
FROM
MyTable
WHERE
....
)
So in essence, you select your max number, then from that selection
you select your final grouping, i.e.,
If these were the records:
1,2,3,4,5,6,7,8 ,9,10,11,12,13, 14,15,16,17,18, 19,20,21,22,23, 24,25,26,27,28, 29,30,31,32,33, 34,35,36,37,38, 39,40,41,42,43, 44,45,46,47,48, 49, ....
the select with the TOP clause limits the data to:
1,2,3,4,5,6,7,8 ,9,10,11,12,13, 14,15,16,17,18, 19,20,21,22,23, 24,25,26,27,28, 29,30,31,32,33, 34,35,36,37,38, 39,40
by getting the top 40 records, then the select with the BOTOM clause
futher limits the data to:
21,22,23,24,25, 26,27,28,29,30, 31,32,33,34,35, 36,37,38,39,40
by getting the last 20 records from the prior select.
I'm sure that there are many newer and better ways too do this now -
especially if you are wanting to use Stored Procs., but this is how to
do it "Old School".
Hope this helps,
L. Lee Saunders
an Oracle shop) but I remember being able to use the TOP and BOTTOM
keywords to get a set of data.
So, if you wanted the 21 thru 40th record:
SELECT BOTTOM 20
*
FROM
(SELECT TOP 40
*
FROM
MyTable
WHERE
....
)
So in essence, you select your max number, then from that selection
you select your final grouping, i.e.,
If these were the records:
1,2,3,4,5,6,7,8 ,9,10,11,12,13, 14,15,16,17,18, 19,20,21,22,23, 24,25,26,27,28, 29,30,31,32,33, 34,35,36,37,38, 39,40,41,42,43, 44,45,46,47,48, 49, ....
the select with the TOP clause limits the data to:
1,2,3,4,5,6,7,8 ,9,10,11,12,13, 14,15,16,17,18, 19,20,21,22,23, 24,25,26,27,28, 29,30,31,32,33, 34,35,36,37,38, 39,40
by getting the top 40 records, then the select with the BOTOM clause
futher limits the data to:
21,22,23,24,25, 26,27,28,29,30, 31,32,33,34,35, 36,37,38,39,40
by getting the last 20 records from the prior select.
I'm sure that there are many newer and better ways too do this now -
especially if you are wanting to use Stored Procs., but this is how to
do it "Old School".
Hope this helps,
L. Lee Saunders
Comment