please check the query clearly. it will delete only others keeping max activity date record intact.see illustration belowcreate table #testtable (Bond_Number int,Activity_Date datetime)insert #testtableSELECT 110,'20100512' union allSELECT 110,'20110902' union allSELECT 110,'20110302' union allSELECT 112,'20101202' union allSELECT 115,'20071223' union allSELECT 110,'20090512' union allSELECT 322,'20111118' union allSELECT 112,'20101108' union allSELECT 115,'20050602' union allSELECT 322,'20090101' SELECT * FROM #testtableDELETE tFROM #testtable tLEFT JOIN (SELECT MAX(Activity_Date) AS MaxActivity_Date, Bond_NumberFROM #testtableGROUP BY Bond_Number)t1On t1.Bond_Number = t.Bond_NumberAND t1.MaxActivity_Date = t.Activity_DateWHERE t1.Bond_Number IS NULLSELECT * FROM #testtabledrop table #testtableoutput------------------------------------before deletion---------------------------------------Bond_Number Activity_Date110 2010-05-12 00:00:00.000110 2011-09-02 00:00:00.000110 2011-03-02 00:00:00.000112 2010-12-02 00:00:00.000115 2007-12-23 00:00:00.000110 2009-05-12 00:00:00.000322 2011-11-18 00:00:00.000112 2010-11-08 00:00:00.000115 2005-06-02 00:00:00.000322 2009-01-01 00:00:00.000after deletion-------------------------------------Bond_Number Activity_Date110 2011-09-02 00:00:00.000112 2010-12-02 00:00:00.000115 2007-12-23 00:00:00.000322 2011-11-18 00:00:00.000
you can clearly see from above that it retains only max records and deletes others------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/