Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / Other Technologies / Full-Text Search / July 2008

Tip: Looking for answers? Try searching our database.

SELECTing a range from the results

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Nathan Sokalski - 16 Jul 2008 01:32 GMT
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.
Signature

Nathan Sokalski
njsokalski@hotmail.com
http://www.nathansokalski.com/

Denny Cherry - 16 Jul 2008 02:08 GMT
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

>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.
Ben Schwehn - 16 Jul 2008 02:10 GMT
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
Signature

Ben Schwehn
bschwehn.de

Madhivanan - 16 Jul 2008 10:19 GMT
> 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
[quoted text clipped - 5 lines]
> Nathan Sokalski
> njsokal...@hotmail.comhttp://www.nathansokalski.com/

Also search for Pagination
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.