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 |
crazycat503
Starting Member
16 Posts |
Posted - 2012-01-27 : 04:16:21
|
I have a single table with 2 fields: id,mid. Id is autonumber while mid is group code.sample data is1 32 33 34 35 36 47 48 49 410 411 4I want a single query that deletes 2 oldest rows for each groupAfter the query, the ff wld remain3 34 35 38 49 410 411 4I don't want to use triggers or so.All guys! |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2012-01-27 : 04:35:44
|
What should be the result if there are for example only two rows for a group in that table? No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-27 : 08:34:13
|
[code]DELETE tFROM(SELECT ROW_NUMBER() OVER (PARTITION BY mid ORDER BY Id) AS Rn FROM table)tWHERE Rn <=2[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
crazycat503
Starting Member
16 Posts |
Posted - 2012-02-01 : 05:03:05
|
Thanks i will try the code...and yes, if the row number is <=2, no effect on it.All guys! |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-01 : 09:30:31
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|