bilgibilisim
bilgibilisim

Reputation: 7

Sql, Top N, Last N Rows Multiple Records

My Query

SELECT *
FROM NumberedTable
WHERE RowNumber <= @firstRowCount 
OR RowNumber IN (SELECT TOP (@lastRowCount) RowNumber  
                 FROM NumberedTable NUI 
                 ORDER BY NUI.RowNumber DESC)

Before this query, another query working. I'm setting row numbers in this query.

But, this query's result top n row and last n row for all record. I need FIRST N rows and LAST N rows for specific column filter in all records.

MY QUERY RESULT PICTURE!

ALL RECORDS PICTURE!

I need first N rows and last N rows for all doctors ! Not use where condition !

Upvotes: 0

Views: 339

Answers (1)

sumit
sumit

Reputation: 15464

MAY BE LIKE THIS?

SELECT TOP N * FROM TBL ORDER BY ID ASC

UNION ALL

SELECT TOP N * FROM TBL ORDER BY ID DESC

Upvotes: 3

Related Questions