I am an not able to build a logic in the below scenario.
I have a table MY_TRANSACTIONS with below data
TXN_ID ACCOUNT OWNER DATE AMOUNT STATUS BEGIN_DT END_DT
1 123 TOM 2/1/16 100 APPROVED 2/1/16
2 123 TOM 2/2/16 200 REJECTED 2/2/16
3 123 TOM 2/2/16 150 PENDING 2/3/16
it has the unique constraint on the (ACCOUNT, DATE,STATUS, END_DT)
I need to implement the logic as below with an array of data as input.
- If the user is trying to approve an pending record I have to update the PENDING status as APPROVED.
- If the user is trying to reject an APPROVED record I have to update the END_DT for that approved record and INSERT a new record with STATUS AS REJECTED.
Here is the Input I will be getting
ACCOUNT OWNER DATE AMOUNT STATUS
123 TOM 2/1/16 100 REJECTED
123 CHRIS 2/5/16 500 PENDING
123 TOM 2/2/16 150 APPROVED
Please let me know if you need any further details.
A set of test case scripts would have been useful.....sigh.
Anyway, try something like this
SQL> drop table MY_TRANSACTIONS purge;
Table dropped.
SQL> create table MY_TRANSACTIONS (
2 seq int, txn_id int, account varchar2(10), dte date, amt int, status varchar2(20), begin_dt date, end_dt date );
Table created.
SQL>
SQL>
SQL> insert into MY_TRANSACTIONS values (1, 123 ,'TOM', '2-feb-16', 100 ,'APPROVED', '1-feb-16',null );
1 row created.
SQL> insert into MY_TRANSACTIONS values (2, 123 ,'TOM', '2-feb-16', 200 ,'REJECTED', '2-feb-16',null);
1 row created.
SQL> insert into MY_TRANSACTIONS values (3, 123 ,'TOM', '2-feb-16', 150 ,'PENDING' ,'3-feb-16',null);
1 row created.
SQL>
SQL> drop table gtt purge;
Table dropped.
SQL>
SQL> create global temporary table gtt (seq int, txn_id int, account varchar2(10), dte date, amt int, status varchar2(20) );
Table created.
SQL>
SQL> insert into gtt values (1,123 ,'TOM', '2-feb-16', 100, 'REJECTED');
1 row created.
SQL> insert into gtt values (2,123 ,'CHRIS', '5-feb-16', 500, 'PENDING');
1 row created.
SQL> insert into gtt values (3,123 ,'TOM', '2-feb-16', 150, 'APPROVED ');
1 row created.
SQL>
SQL> update MY_TRANSACTIONS
2 set status = 'APPROVED'
3 where status = 'PENDING'
4 and (txn_id,account) in ( select txn_id,account from gtt where status = 'APPROVED' );
0 rows updated.
SQL>
SQL>
SQL> update MY_TRANSACTIONS m
2 set end_dt = ( select dte from gtt where txn_id = m.txn_id and account = m.account and m.status = 'REJECTED' )
3 where status = 'APPROVED'
4 and (txn_id,account) in ( select txn_id,account from gtt where status = 'REJECTED' );
1 row updated.
SQL>
SQL> insert into MY_TRANSACTIONS
2 select seq , txn_id , account , dte , amt , status, dte, null
3 from gtt where status = 'REJECTED';
1 row created.
SQL>
SQL> select * from MY_TRANSACTIONS;
SEQ TXN_ID ACCOUNT DTE AMT STATUS BEGIN_DT END_DT
---------- ---------- ---------- --------- ---------- -------------------- --------- ---------
1 123 TOM 02-FEB-16 100 APPROVED 01-FEB-16
2 123 TOM 02-FEB-16 200 REJECTED 02-FEB-16
3 123 TOM 02-FEB-16 150 PENDING 03-FEB-16
1 123 TOM 02-FEB-16 100 REJECTED 02-FEB-16
SQL>