SQL Server 2012 is very easy
SELECT col1, col2, ... FROM ... WHERE ... ORDER BY
If we want to skip ORDER BY, we can use
SELECT col1, col2, ... ... ORDER BY CURRENT_TIMESTAMP OFFSET 10 ROWS
(I would rather note this as a hack, but it was used, for example, by NHibernate. Using a reasonably matched column as ORDER BY is the preferred way)
to answer the question:
--SQL SERVER 2012 SELECT PostId FROM ( SELECT PostId, MAX (Datemade) as LastDate from dbForumEntry group by PostId ) SubQueryAlias order by LastDate desc OFFSET 10 ROWS -- skip 10 rows FETCH NEXT 10 ROWS ONLY; -- take 10 rows
The new offset and fetch next keywords were introduced (only the following SQL standards).
But I assume that you are not using SQL Server 2012 , right? In the previous version, this is a bit (a bit) complicated. Here is a comparison and examples for all versions of SQL Server: here
So this may work in SQL Server 2008 :
-- SQL SERVER 2008 DECLARE @Start INT DECLARE @End INT SELECT @Start = 10,@End = 20; ;WITH PostCTE AS ( SELECT PostId, MAX (Datemade) as LastDate ,ROW_NUMBER() OVER (ORDER BY PostId) AS RowNumber from dbForumEntry group by PostId ) SELECT PostId, LastDate FROM PostCTE WHERE RowNumber > @Start AND RowNumber <= @End ORDER BY PostId
Radim Köhler Nov 04 '12 at 18:35 2012-11-04 18:35
source share