Welcome to dbFreaks.com!
FAQFAQ      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

SELECTing a range from the results

 
   Database Help (Home) -> Full Text RSS
Next:  Full text index not created on varbinary(max) - s..  
Author Message
Nathan Sokalski

External


Since: Jul 15, 2008
Posts: 4



(Msg. 1) Posted: Tue Jul 15, 2008 8:32 pm
Post subject: SELECTing a range from the results
Archived from groups: microsoft>public>sqlserver>fulltext, others (more info?)

I would like to create a SELECT statement that returns a certain range of
the results that a WHERE clause returns. For example, if the WHERE clause
would return 75 records, I want a way to have the SELECT statement return
the second 10 records, or the third 10 records, etc. I know how to use the
TOP(x) clause, but that alone always returns the first x records. Is there a
way to offset the starting point for which records to return? Thanks.
--
Nathan Sokalski
njsokalski DeleteThis @hotmail.com
http://www.nathansokalski.com/

 >> Stay informed about: SELECTing a range from the results 
Back to top
Login to vote
Denny Cherry

External


Since: Jul 15, 2008
Posts: 55



(Msg. 2) Posted: Tue Jul 15, 2008 8:32 pm
Post subject: Re: SELECTing a range from the results [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

We use a technique using a CTE in combination with the ROW_NUMBER
function to do this. Something like this.

@PageSize and @PageIndex are passed into the procedure.

SET @PageLowerBound = @PageSize * @PageIndex + 1;
SET @PageUpperBound = @PageLowerBound + @PageSize - 1;

WITH AllRows AS (
SELECT ROW_NUMBER() OVER (ORDER BY SortedColumn ASC)
ROW_NUMBER,
AnotherColumn,
ThirdColumn
FROM YourTable)

SELECT *
FROM AllRows
WHERE ROW_NUMBER BETWEEN @PageLowerBound AND @PageUpperBound

I hope this helps.

Denny

On Tue, 15 Jul 2008 20:32:17 -0400, "Nathan Sokalski"
<njsokalski DeleteThis @hotmail.com> wrote:

>I would like to create a SELECT statement that returns a certain range of
>the results that a WHERE clause returns. For example, if the WHERE clause
>would return 75 records, I want a way to have the SELECT statement return
>the second 10 records, or the third 10 records, etc. I know how to use the
>TOP(x) clause, but that alone always returns the first x records. Is there a
>way to offset the starting point for which records to return? Thanks.

 >> Stay informed about: SELECTing a range from the results 
Back to top
Login to vote
Ben Schwehn

External


Since: Jul 15, 2008
Posts: 1



(Msg. 3) Posted: Tue Jul 15, 2008 8:32 pm
Post subject: Re: SELECTing a range from the results [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

x-post and follow-up set to microsoft.public.sqlserver.programming

> I would like to create a SELECT statement that returns a certain range
> of the results that a WHERE clause returns. For example, if the WHERE
> clause would return 75 records, I want a way to have the SELECT
> statement return the second 10 records, or the third 10 records, etc.

first way i can think of is simply to combine 2 top queries like

select top 10 * from (select top 65 * from table order by ordercol desc)
foo order by ordercol asc

second is to use the row_number function to and use this in a where clause
something like:

select * from (
select row_number() over (order by Id desc) as foo from table
) bar where foo between 10 and 20

I guess both options above will not perform too well though, but as long
as your tables aren't huge, that might not a big issue. For large tables,
perhaps adding a column with the rank/row_number that you can then use in
the where clause would be a solution?



hth
Ben
--
Ben Schwehn
bschwehn.de
 >> Stay informed about: SELECTing a range from the results 
Back to top
Login to vote
Madhivanan

External


Since: Jun 05, 2008
Posts: 13



(Msg. 4) Posted: Wed Jul 16, 2008 2:19 am
Post subject: Re: SELECTing a range from the results [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Jul 16, 5:32 am, "Nathan Sokalski" <njsokal... DeleteThis @hotmail.com> wrote:
> I would like to create a SELECT statement that returns a certain range of
> the results that a WHERE clause returns. For example, if the WHERE clause
> would return 75 records, I want a way to have the SELECT statement return
> the second 10 records, or the third 10 records, etc. I know how to use the
> TOP(x) clause, but that alone always returns the first x records. Is there a
> way to offset the starting point for which records to return? Thanks.
> --
> Nathan Sokalski
> njsokal... DeleteThis @hotmail.comhttp://www.nathansokalski.com/


Also search for Pagination
 >> Stay informed about: SELECTing a range from the results 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
funny search results with 'contains' clause - hi, it is really funny how ft search works: it seems to depend highly on what could be indexed, but it still doesn't follow a determined order. consider the following queries: select name from person where contains(name, 'hammer') select name from..

Incorrect RANK results returned - Morning all, I'm having problems with my FTS rankings on our SQL2005 database and I'm a bit stumped as where to start looking at what the the problem might be. The simplified SQL is as follows:..

Insert Into Without Log - Hi, Can I create an Insert Into Query without sql server registration log? Thx.

SQL 2K : custom word breaker ? - Hi, Is it possible to create a custom word breaker for SQLServer fulltext.... I use french word breaker, but for some specific tables, it's not exactly what I want... Seems such a dream will be feasible in SQL 2K5. Please anyone who knows, tell me the....

restore to another server of db with FT catalogs - If we restore a backup copy of a production db having FT catalogs onto a laptop running SQL Server Developer version, the restored db will contain references to FT catalogs whose underlying system files are not yet present on the laptop (because backup..
   Database Help (Home) -> Full Text All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]