2012-08-06 45 views
2

我有一個類似於此的表。SQL查找列停止遞增的行

creation_time   po_S   count_out_I 
2012-08-03 02:45:02.133 000002029382 15974 
2012-08-03 02:48:02.083 000002029382 9475 
2012-08-03 02:51:02.097 000002029382 15978 
2012-08-03 02:54:02.120 000002029382 15990 

我需要找到count_out_I小於前一行時按creation_time排序的行。 數據庫是SQL Server 2008.幫助表示讚賞。

回答

4

這將第一行隨count_out_I報告:

; with numbered as 
     (
     select row_number() over (order by creation_time) as rn 
     ,  * 
     from YourTable 
     ) 
select top 1 cur.* 
from numbered cur 
join numbered prev 
on  prev.rn + 1 = cur.rn 
where cur.count_out_I < prev.count_out_I 
order by 
     cur.rn