Skip to content
Advertisement

PL/SQL: procedure to process a comparison between two tables

This is an example what i need :

Table 1 :

Id  champ1 champ2 champ3
   1      A      S      Q
   2      D      K      A
   3      D      A      S

Table 2:

Id  champ1 champ2 champ3
   1      A      S      Q
   2      Q      A      A
   3      A      A      K  

A procedure that feeds a table3 as a result of comparison between the table 1 and table 2

Table 3:

Id_exc name_champ noted_val except_val
   2    champ1       D         Q
   3    champ1       D         A
   2    champ2       K         A
   3    champ3       S         K

Please, i need help if someone had the same challenge 🙂

Advertisement

Answer

Data setup:

CREATE TABLE Table1
    (Id int, champ1 varchar2(1), champ2 varchar2(1), champ3 varchar2(1))
;

INSERT ALL 
    INTO Table1 (Id, champ1, champ2, champ3)
         VALUES (1, 'A', 'S', 'Q')
    INTO Table1 (Id, champ1, champ2, champ3)
         VALUES (2, 'D', 'K', 'A')
    INTO Table1 (Id, champ1, champ2, champ3)
         VALUES (3, 'D', 'A', 'S')
SELECT * FROM dual
;

CREATE TABLE Table2
    (Id int, champ1 varchar2(1), champ2 varchar2(1), champ3 varchar2(1))
;

INSERT ALL 
    INTO Table2 (Id, champ1, champ2, champ3)
         VALUES (1, 'A', 'S', 'Q')
    INTO Table2 (Id, champ1, champ2, champ3)
         VALUES (2, 'Q', 'A', 'A')
    INTO Table2 (Id, champ1, champ2, champ3)
         VALUES (3, 'A', 'A', 'K')
SELECT * FROM dual
;

The below query would give you the differences

SELECT * 
FROM   ((SELECT * 
         FROM   table1 
         minus 
         SELECT * 
         FROM   table2) 
        UNION 
        (SELECT * 
         FROM   table2 
         minus 
         SELECT * 
         FROM   table1)); 

ID  CHAMP1  CHAMP2  CHAMP3
2   D   K   A
2   Q   A   A
3   A   A   K
3   D   A   S
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement