amid
amid

Reputation: 23

SELECT first 3 rows after 8th row

I have a table: id, Group, Status, Subject.

I need rows where group=2 AND status=2, but I want to skip the first 8 results (ordered by id). This is what I tried, but it does not skip the first 8 rows:

SELECT TOP 3 [id], LEFT ([subject],30) AS Title 
FROM [Katavot] 
    WHERE (([Status] = @Status) AND ([Group] = @Group)) 
ORDER BY [id] DESC

Upvotes: 1

Views: 1375

Answers (3)

Pavel Nefyodov
Pavel Nefyodov

Reputation: 896

;WITH Katavod_Modified AS 
(
    SELECT id, LEFT ([subject],30) As Subject, [Group], [Status],  ROW_NUMBER() OVER (ORDER BY id) RN
    FROM [Katavot]
)
SELECT TOP 3 [id], LEFT ([subject],30) AS Title FROM [Katavod_Modified] WHERE 
(([Status] = @Status) AND ([Group] = @Group) AND (RN > 8)) ORDER BY [id] DESC 

Looks very similar to this one.

Upvotes: 1

NaN
NaN

Reputation: 9104

MySQL:

LIMIT x, y

MSSQL:

select top y *
from <table>
where <unique id> not in (select top x <unique id> from <table> order by <unique id>)
from <table> order by <unique id>

Upvotes: -1

user1877337
user1877337

Reputation:

Based on row_number

WITH MyResult AS
{
SELECT ROW_NUMBER() OVER(ORDER BY Id DESC) AS RowNumber, [id], LEFT ([subject],30) AS Title 
FROM [Katavot] 
    WHERE (([Status] = @Status) AND ([Group] = @Group)) 
ORDER BY [id] DESC
}
SELECT [id], Title 
FROM MyResult
WHERE RowNumber BETWEEN 9 AND 12

Upvotes: 1

Related Questions