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)
 Table cleanup

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-06-28 : 09:46:54
Vahit Kis writes "Hi,
I have a very large table which keeps card transactions.
I am trying to write a cleanup function which deletes records
older than 6 months. But there is a restriction here.
I have to keep the latest transaction of a card even if that
transaction is older than 6 months.
For example,
A card has 10 transactions and all of them are older than 6 months. I have to keep the latest transaction and delete the other 9 transactions.
What is the best and fast way to do this task?
Best regards.

Vahit KIS"

setbasedisthetruepath
Used SQL Salesman

992 Posts

Posted - 2002-06-28 : 10:55:24
quote:

Vahit Kis writes "Hi,
I have a very large table which keeps card transactions.
I am trying to write a cleanup function which deletes records
older than 6 months. But there is a restriction here.
I have to keep the latest transaction of a card even if that
transaction is older than 6 months.
For example,
A card has 10 transactions and all of them are older than 6 months. I have to keep the latest transaction and delete the other 9 transactions.
What is the best and fast way to do this task?
Best regards.

Vahit KIS"



You haven't posted DDL so I can't write the exact DML statement for you, but generally:

delete transactions
from transactions t
where datediff( m, trandate, current_timestamp ) >= 6 and exists (
select 1
from transactions
where accountno = t.accountno and trandate > t.trandate )

in pseudo-sql: delete transactions older than 6 months where there exists a more recent transaction for the same account.

Jonathan Boott, MCDBA
Go to Top of Page
   

- Advertisement -