the same as http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=200763;WITH cteSampleAS(SELECT ID,LOAN_NBR,CURRENT_PRINCIPAL,LOAD_DT,LOAN_STATUS FROM (VALUES(1,'00001',1000,'2010-12-5','APPROVED') ,(2,'00001',800,'2010-12-30','APPROVED') ,(3,'00001',0,'2011-3-1', NULL) ,(4,'00002',500,'2012-5-1','APPROVED') ,(5,'00002',0,'2012-6-12','APPROVED') ,(6,'00003',850,'2012-1-1','Pending') ,(7,'00003',500,'2012-2-1','Approved') ,(8,'00003',0,'2012-3-2','Approved') ) A (ID,LOAN_NBR,CURRENT_PRINCIPAL,LOAD_DT,LOAN_STATUS) )SELECT ID ,LOAN_NBR ,CURRENT_PRINCIPAL ,LOAD_DT ,A.LOAN_STATUS ,B.LOAN_STATUS FROM cteSample AS A OUTER APPLY ( SELECT TOP(1) B.LOAN_STATUS FROM cteSample AS B WHERE A.LOAN_NBR = B.LOAN_NBR AND A.ID > B.ID ORDER BY B.ID DESC )BWHERE A.CURRENT_PRINCIPAL =0 AND ( (A.LOAN_STATUS <> B.LOAN_STATUS) OR (A.LOAN_STATUS IS NULL AND B.LOAN_STATUS IS NOT NULL) )ORDER BY A.ID
sabinWeb MCP