3

I am trying to setup an Offset and Limit, I have tried the following:

SELECT
    [Job_No]
,   ROW_NUMBER() OVER (ORDER BY [Job_No]) AS [rownumber]
,   [BaselineStart]
,   [BaselineFinish]
,   [ExpectedStart]
,   [ExpectedFinish]
,   [ScheduledStart]
,   [ScheduledFinish]
,   [ActualStart]
,   [ActualFinish]
FROM
    [Schedule]
WHERE
    [rownumber] BETWEEN 10 AND 20;

but I get this error:

Invalid column name 'rownumber'.

Please help.

2
  • 1
    You can't reference window function in the where clause. You will have to either move your query to a subquery or more it to a cte. A little bit of formatting would be a huge help for this and all your queries. All smashed like this is very challenging to read.
    – Sean Lange
    Feb 3, 2016 at 16:20
  • You need a nested query to use rownumber Feb 3, 2016 at 16:21

4 Answers 4

7

In SQL 2012+ you can use the new OFFSET and FETCH NEXT arguments in the ORDER BY clause:

SELECT Columns
FROM MyTable
ORDER BY SomeColumn
OFFSET 10 ROWS --this means start with row 11
FETCH NEXT 10 ROWS ONLY  --this means limit the results to the next 10 rows.

So the above query will return rows 11 - 20, ordered by SomeColumn

3
  • I tried this and got this error: Incorrect syntax near '10'.
    – user979331
    Feb 3, 2016 at 16:39
  • I forgot to add the ORDER BY but when I do I get this error: Msg 102, Level 15, State 1, Line 2 Incorrect syntax near 'OFFSET'. Msg 153, Level 15, State 2, Line 2 Invalid usage of the option NEXT in the FETCH statement.
    – user979331
    Feb 3, 2016 at 16:43
  • Gotta be another typo, my example is syntactically correct. Feb 3, 2016 at 16:53
5

You should add another step to your query. ROW_NUMBER() can't be used in the same query in which you define it, so you have to wrap it all in another select like below. The where clause relates to all the fields in the FROM table/query, so rownumber doesn't exist yet.

select * from
(SELECT Job_No,
        ROW_NUMBER() OVER(ORDER BY Job_No) AS rownumber,
        BaselineStart, BaselineFinish, ExpectedStart, ExpectedFinish,
        ScheduledStart, ScheduledFinish, ActualStart, ActualFinish
        FROM Schedule)
WHERE rownumber BETWEEN 10 AND 20
3

From SQLServer 2012 onwards ,Since version is not specified

below is a version using offset and fetch,below code will ignore first 10 rows and get next 10 rows ordered by jobno

SELECT Job_No, 
      BaselineStart, 
   BaselineFinish, 
   ExpectedStart, 
   ExpectedFinish, 
   ScheduledStart, 
   ScheduledFinish, 
   ActualStart, 
   ActualFinish 
FROM Schedule
order by job_no
offset  10 rows fetch next 10 rows only;
1
  • 1
    @Dika: Updated my answer to reflect that Nov 30, 2018 at 13:25
2

Using CTE

WITH Result AS 
(
SELECT Job_No, 
   ROW_NUMBER() OVER(ORDER BY Job_No) AS rownumber, 
   BaselineStart, 
   BaselineFinish, 
   ExpectedStart, 
   ExpectedFinish, 
   ScheduledStart, 
   ScheduledFinish, 
   ActualStart, 
   ActualFinish 
FROM Schedule
)
SELECT  *
FROM    Result
WHERE   rownumber BETWEEN 10 AND 20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.