sql - Delete All Rows apart from highest date -


i have table looks this.

id | create date ---+------------------------- 1  | 2007-06-12 15:44:10:000 1  | 2007-07-12 15:44:10:000 2  | 2007-06-12 15:44:10:000 2  | 2007-07-12 15:44:10:000 2  | 2007-08-12 15:44:10:000 3  | 2007-09-12 15:44:10:000 4  | 2012-06-12 15:44:10:000 5  | 2011-07-12 15:44:10:000 

i need keep max(createdate) while deleting other rows. need keep rows id 3,4,5.

any great.

note: little abstract. actual table has millions of ids have many duplicate ids different create dates

you can use row_number() identify records have recent date, , delete rest:

;with todelete (     select  *,             row_number() on (partition id order createdate desc) rn        yourtable ) delete  todelete   rn <> 1 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -