Operating System - HP-UX
1748177 Members
4300 Online
108758 Solutions
New Discussion

Re: pl/sql proc - delete records

 
Nirmalkumar
Frequent Advisor

pl/sql proc - delete records

Hello all,

iam not much familiar on pl/sql scripting.could you please give me an idea for to write pl/sql script for the below requirement in easier method

Keep two recent records of each employee id ,delete rest of the records.

Attached table structure with this thread.

In the table structure,need to keep two recent records of each column 'EMPLID' using the date column 'AS_OF_DATE'

total count of records of the table - around 1 million.


Thanks,
Nirmal

1 REPLY 1
Volker Borowski
Honored Contributor

Re: pl/sql proc - delete records

Hello,

some comments:

The structure appears to be a copied SAP table (concluded from column MANDT).
If yes, validate the following:

1) Within SAP usually all/most Columns should carry real values and no NULL-values.
Your structure does not show any NOT-NULL constraints, so be sure to validate, that this copy-table does not have null values that could do any damage to your required result.

2) Column MANDT is used in SAP to seperate data. Make sure, that either MANDT contains a single uniqe value, or if you need to group by or select a specific MANDT (which value ?) in addition. otherwise you might get mixed up values of diffrent clients (MANDT) with no logical sense if you do no diversification with the value of MANDT.

3) It is always very expensive and complicate to delete most of the rows of a table. Instead of delete-ing many rows and keep few, copy a few rows to a new table, "truncate" source and copy back.
This is easier to handle in terms of LOCKs, redo, undo and memory.

4) can you have more than one row result for
(MANDT)+EMPLID+AS_OF_DATE ? Which one do you like to keep if this is the case ?

select EMPLID,AS_OF_DATE ...
group by EMPLID,AS_OF_DATE
having count(*) > 1;

or (if MANDT is relevant)

select MANDT,EMPLID,AS_OF_DATE ...
group by MANDT,EMPLID,AS_OF_DATE
having count(*) > 1;

Should give you the result, if you have to do additional diversification.


5) To select the required data, you could use
oracle analytical functions.

select
...list_all_colums...,
ROW_NUMBER() OVER(PARTITION BY EMPLID ORDER BY AS_OF_DATE DESC) group_num
from ...
where
-- /* may be */ MANDT = '001' AND
group_num <=2;

If this result fits your needs create the result table.

create table result_table
as select ...all_required_columns...
from ( select ... as before )

Hope this gives you a starting point to solve the problem.

Volker