Контрольный пример:
SQL> create table ctrl_result
2 (seq_id number,
3 ctrl_id varchar2(4),
4 c1 varchar2(3),
5 c2 number,
6 status varchar2(5));
Table created.
SQL> create table ctrl_mstr
2 (ctrl_id varchar2(4),
3 tolerance number,
4 symbol varchar2(2));
Table created.
SQL> insert into ctrl_result
2 select 1, 'C001', 'DES', 380, null from dual union all
3 select 2, 'C001', 'ABC', 0, null from dual union all
4 select 3, 'C001', 'EDC', 0, null from dual union all
5 select 4, 'C002', 'XXX', 500, null from dual union all
6 select 5, 'C002', 'YYY', 100, null from dual;
5 rows created.
SQL> insert into ctrl_mstr
2 select 'C001', 1000, '<' from dual union all
3 select 'C002', 200, '>' from dual;
2 rows created.
Некоторые запросы и результат:
SQL> merge into ctrl_result c
2 using (select r.seq_id, r.ctrl_id, r.c2,
3 m.tolerance, m.symbol,
4 --
5 case when m.symbol = '<' and r.c2 < m.tolerance then 'PASS'
6 when m.symbol = '<' and r.c2 >= m.tolerance then 'FAIL'
7 when m.symbol = '>' and r.c2 > m.tolerance then 'PASS'
8 when m.symbol = '>' and r.c2 <= m.tolerance then 'FAIL'
9 else 'NONE'
10 end status
11 from ctrl_result r join ctrl_mstr m on m.ctrl_id = r.ctrl_id
12 ) x
13 on (c.seq_id = x.seq_id)
14 when matched then update set c.status = x.status;
5 rows merged.
SQL> select * From ctrl_Result order by seq_id;
SEQ_ID CTRL C1 C2 STATU
---------- ---- --- ---------- -----
1 C001 DES 380 PASS
2 C001 ABC 0 PASS
3 C001 EDC 0 PASS
4 C002 XXX 500 PASS
5 C002 YYY 100 FAIL
SQL>