| 
                
Please start any new threads on our new 
    site at https://forums.sqlteam.com.  We've got lots of great SQL Server
    experts to answer whatever question you can come up with. 
    
        | 
                
                    | 
                            
                                | Author | Topic |  
                                    | GetzinStarting Member
 
 
                                        6 Posts | 
                                            
                                            |  Posted - 2009-09-09 : 07:27:45 
 |  
                                            | I have a table with following columns:ClientCode, ChangeDateTime, OldPassword.As passwords are changed, the oldpassword and datetime as saved in the table. I need a select stmt to get the last N records per client.Or a delete stmt which keeps the last N records per client and deletes rest. |  |  
                                    | senthil_nagoreMaster Smack Fu Yak Hacker
 
 
                                    1007 Posts | 
                                        
                                          |  Posted - 2009-09-09 : 07:40:30 
 |  
                                          | select top <N> * from table where ClientCode= <ClientCode>order by ChangeDateTimeORDelete From table where not in(select top <N> * from table where ClientCode= <ClientCode>order by ChangeDateTime)Senthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/ |  
                                          |  |  |  
                                    | GetzinStarting Member
 
 
                                    6 Posts | 
                                        
                                          |  Posted - 2009-09-09 : 07:47:33 
 |  
                                          | Hi Senthil,Thank for your answer, but it won't satisfy my requirement.I want to keep last N records for every Client, and drop the rest. |  
                                          |  |  |  
                                    | bklrMaster Smack Fu Yak Hacker
 
 
                                    1693 Posts | 
                                        
                                          |  Posted - 2009-09-09 : 08:20:12 
 |  
                                          | see this linkhttp://sqlblogcasts.com/blogs/madhivanan/archive/2008/09/12/return-top-n-rows.aspx |  
                                          |  |  |  
                                    | ra.shindePosting Yak  Master
 
 
                                    103 Posts | 
                                        
                                          |  Posted - 2009-09-09 : 10:31:51 
 |  
                                          | DECLARE @tab TABLE (ClientCode INT , ChangeDateTime datetime, OldPassword VARCHAR(10))INSERT INTO @tabSELECT 1, getdate(), 'abc1'UNION ALLSELECT 1, getdate()-2, 'abc2'UNION ALLSELECT 1, getdate()-3, 'abc3'UNION ALLSELECT 1, getdate()-4, 'abc4'UNION ALLSELECT 2, getdate(), 'abc1'UNION ALLSELECT 2, getdate()-2, 'abc2'UNION ALLSELECT 2, getdate()-3, 'abc3'UNION ALLSELECT 2, getdate()-4, 'abc4'UNION ALLSELECT 3, getdate(), 'abc1'UNION ALLSELECT 3, getdate()-2, 'abc2'UNION ALLSELECT 3, getdate()-3, 'abc3'UNION ALLSELECT 3, getdate()-4, 'abc4'SELECT * FROM(SELECT row_number() OVER (partition BY ClientCode ORDER BY ChangeDateTime DESC) RN,  * FROM @tab)aWHERE RN < 3Rahul Shinde |  
                                          |  |  |  
                                    | madhivananPremature Yak Congratulator
 
 
                                    22864 Posts | 
                                        
                                          |  Posted - 2009-09-10 : 02:36:42 
 |  
                                          | quote:Note that this is posted in SQL Server 2000 forumMadhivananFailing to plan is Planning to failOriginally posted by ra.shinde
 DECLARE @tab TABLE (ClientCode INT , ChangeDateTime datetime, OldPassword VARCHAR(10))INSERT INTO @tabSELECT 1, getdate(), 'abc1'UNION ALLSELECT 1, getdate()-2, 'abc2'UNION ALLSELECT 1, getdate()-3, 'abc3'UNION ALLSELECT 1, getdate()-4, 'abc4'UNION ALLSELECT 2, getdate(), 'abc1'UNION ALLSELECT 2, getdate()-2, 'abc2'UNION ALLSELECT 2, getdate()-3, 'abc3'UNION ALLSELECT 2, getdate()-4, 'abc4'UNION ALLSELECT 3, getdate(), 'abc1'UNION ALLSELECT 3, getdate()-2, 'abc2'UNION ALLSELECT 3, getdate()-3, 'abc3'UNION ALLSELECT 3, getdate()-4, 'abc4'SELECT * FROM(SELECT row_number() OVER (partition BY ClientCode ORDER BY ChangeDateTime DESC) RN,  * FROM @tab)aWHERE RN < 3Rahul Shinde
 
 |  
                                          |  |  |  
                                    | GetzinStarting Member
 
 
                                    6 Posts | 
                                        
                                          |  Posted - 2009-09-10 : 02:44:35 
 |  
                                          | Your code 1 works for me. Thanks Madhivannan.   |  
                                          |  |  |  
                                    | madhivananPremature Yak Congratulator
 
 
                                    22864 Posts | 
                                        
                                          |  Posted - 2009-09-10 : 02:55:31 
 |  
                                          | quote:You are welcomeOriginally posted by Getzin
 Your code 1 works for me. Thanks Madhivannan.
  
  MadhivananFailing to plan is Planning to fail |  
                                          |  |  |  
                                |  |  |  |  |  |