you can use ROW_NUMBER() to get a strict sequence and then join it to itself to compare the row before. Here I've used a CTE (though you don't need to)DECLARE @foo TABLE ( [val] CHAR(1) , [timestamp] DATETIME )INSERT @foo SELECT 'a', '2009-01-01T11:31:00'UNION ALL SELECT 'b', '2009-01-01T12:00:00'UNION ALL SELECT 'c', '2009-03-01T12:00:00'UNION ALL SELECT 'd', '2009-03-01T17:00:00'UNION ALL SELECT 'e', '2008-01-01T12:00:00'UNION ALL SELECT 'f', '2008-01-01T12:20:12'UNION ALL SELECT 'g', '2008-01-01T12:50:00'-- Show all rows from @fooSELECT * FROM @foo-- Select rows that are within 30 mins of the row before (ordered by val ASC);WITH orders AS (SELECT ROW_NUMBER() OVER(ORDER BY [val] ASC) AS [pos] , [val] AS [val] , [timeStamp] AS [timestamp]FROM @foo )SELECT a.*FROM orders a JOIN orders b ON b.[pos] = a.[pos] + 1 AND DATEDIFF(MINUTE, a.[timeStamp], b.[timeStamp]) BETWEEN 0 AND 31
Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION