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.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Opposite of TOP

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2001-10-09 : 09:30:26
Ron writes "I want to only keep around 5 latest records in my database. On the 6th entry, I want it to replace entry one. I know I can select the TOP 5, but how do i delete the records after the top 5? Keep the TOP 5, delete rest?

Thanks!"

smartlizard
Starting Member

24 Posts

Posted - 2001-12-30 : 19:45:23
Thank you! It worked!

Ron Sell

Ron Sell
http://smartLIZARD.com
Go to Top of Page

Nazim
A custom title

1408 Posts

Posted - 2001-12-31 : 00:00:16
quote:

join to a derived table

delete tbl
from tbl
where not exists
(select * from (select top 5 * from tbl order by date desc) as t2 where tbl.fld1 = t2.fld1 and tbl.fld2 = t2.fld2 and ...))

(Could use left outer join to do same thing).




nr , dont u think u the syntax of delete is wrong in this . i think you are still in a holiday mood [;]) . otherwise nr making mistake's ....never heard



-------------------------
Graz's Baby is my Master:)
Go to Top of Page

Nazim
A custom title

1408 Posts

Posted - 2001-12-31 : 00:00:46
oops! wanted to edit my post and clicked on the wrong button.
yeah "To Err is Human"

----
delete from tbl
where not exists
(select * from (select top 5 * from tbl order by date desc) as t2 where tbl.fld1 = t2.fld1 and tbl.fld2 = t2.fld2 and ...))

----

Edited by - Nazim on 12/31/2001 00:03:27
Go to Top of Page
   

- Advertisement -