Home » SQL & PL/SQL » SQL & PL/SQL » Merge using row order (11.2.0.1.0)
Merge using row order [message #639699] Tue, 14 July 2015 06:47 Go to next message
OraFerro
Messages: 433
Registered: July 2011
Senior Member
Hi All,

I have the below case where I need to fill a fk (fk_source_tran) in table try_int from the other table try_trans.
The criteria is matching fk_ln_no, and date between the two tables. However since the number of rows in each table do not match I need to take to consider the first matching rows from fk_source_tran (order by type and id) and leave the others.
I tried to create the ordr field and use the merge as below but failed due to multiple records match, any ideas?:
create table try_int
  (
   int_id number primary key,
   fk_ln_no number,  
   int_amount number,
   int_date date
  );

create table try_trans
  (
  trans_id number primary key,
  fk_ln_no number,
  trans_type number,
  trans_amout number,
  trans_date date
  );

insert all
  INTO try_int values (1, 100, 0, to_date('01-01-2000','dd-mm-yyyy'))
  INTO try_int values (2, 100, 0, to_date('01-01-2000','dd-mm-yyyy'))
  INTO try_int values (3, 100, 5055, to_date('01-01-2000','dd-mm-yyyy'))
  INTO try_int values (4, 100, 0, to_date('01-01-2001','dd-mm-yyyy'))
  INTO try_int values (5, 100, 0, to_date('01-01-2001','dd-mm-yyyy'))
  INTO try_int values (6, 100, 4320, to_date('01-01-2001','dd-mm-yyyy'))
  INTO try_int values (7, 200, 0, to_date('01-01-2001','dd-mm-yyyy'))
  INTO try_int values (8, 200, 8690, to_date('01-01-2001','dd-mm-yyyy'))
  into try_trans values (101, 100,2, 300000, to_date('01-01-2000','dd-mm-yyyy'))
  into try_trans values (102, 100,2, 320000, to_date('01-01-2000','dd-mm-yyyy'))
  into try_trans values (103, 100,7, 340000, to_date('01-01-2000','dd-mm-yyyy'))
  into try_trans values (104, 100,6, 300000, to_date('01-01-2000','dd-mm-yyyy'))
  into try_trans values (105, 100,7, 100000, to_date('01-01-2001','dd-mm-yyyy'))
  into try_trans values (106, 100,6, 100000, to_date('01-01-2001','dd-mm-yyyy'))
  into try_trans values (109, 100,2, 100000, to_date('01-01-2001','dd-mm-yyyy'))
  into try_trans values (124, 100,6, 100000, to_date('01-01-2001','dd-mm-yyyy'))
  into try_trans values (600, 200,6, 200000, to_date('01-01-2001','dd-mm-yyyy'))
  into try_trans values (606, 200,7, 200000, to_date('01-01-2001','dd-mm-yyyy'))
  into try_trans values (609, 200,2, 200000, to_date('01-01-2001','dd-mm-yyyy'))
select * from dual;

alter table try_int add fk_source_tran number references try_trans;


My trial that fails (unstable version of try_int) due to multiple matches:

MERGE into (select rank() over ( PARTITION by fk_ln_no, int_DATE order by int_id) ordr, 
                 try_int.*
             from try_int
            ) a
       using (select rank() over ( PARTITION by fk_ln_no, trans_DATE order by trans_type, trans_id) ordr,
                   try_trans.*
               from try_trans
               where trans_type in (2,6,7)
              ) b
        on (a.fk_ln_no = b.fk_ln_no 
            and a.ordr = b.ordr 
            and a.int_DATE= b.trans_DATE)
WHEN MATCHED THEN UPDATE SET a.fk_source_tran =b.trans_id;


May thanks,
Ferro
Re: Merge using row order [message #639700 is a reply to message #639699] Tue, 14 July 2015 07:39 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
My trial that fails (unstable version of try_int) due to multiple matches:


Not true:
SQL> MERGE into (select rank() over ( PARTITION by fk_ln_no, int_DATE order by int_id) ordr,
  2                   try_int.*
  3               from try_int
  4              ) a
  5         using (select rank() over ( PARTITION by fk_ln_no, trans_DATE order by trans_type, trans_id) ordr,
  6                     try_trans.*
  7                 from try_trans
  8                 where trans_type in (2,6,7)
  9                ) b
 10          on (a.fk_ln_no = b.fk_ln_no
 11              and a.ordr = b.ordr
 12              and a.int_DATE= b.trans_DATE)
 13  WHEN MATCHED THEN UPDATE SET a.fk_source_tran =b.trans_id;
MERGE into (select rank() over ( PARTITION by fk_ln_no, int_DATE order by int_id) ordr,
           *
ERROR at line 1:
ORA-01732: data manipulation operation not legal on this view


You cannot update a view using analytic function.

Re: Merge using row order [message #639733 is a reply to message #639700] Tue, 14 July 2015 17:58 Go to previous messageGo to next message
OraFerro
Messages: 433
Registered: July 2011
Senior Member
Thanks Michel.
I was posting my failed trial to show what i want.
What is the recommended solution in order to update the fk?
Thanks
Ferro
Re: Merge using row order [message #639737 is a reply to message #639733] Wed, 15 July 2015 00:02 Go to previous message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

The recommended solution is to specify what you want to do with several same ".fk_ln_no = b.fk_ln_no and a.int_DATE= b.trans_DATE".
In other words, you have to define conditions that uniquely define rows to join try_int and try_trans.

Previous Topic: Same serial for a group
Next Topic: match/merge steps
Goto Forum:
  


Current Time: Fri Apr 26 09:01:06 CDT 2024