Сначала вы нормализуете эту таблицу состояния с помощью UNION, а затем создаете группу с максимальными значениями.
select status_id, max(levelid), max(levels) from (
select * from status_level, status where desc = desc1
union
select * from status_level, status where desc = desc2
union
select * from status_level, status where desc = desc3
union
select * from status_level, status where desc = desc4)
group by status_id;
Вот весь тестовый скрипт и вывод
create table status (status_id number, desc1 varchar2(50), desc2 varchar2(50), desc3 varchar2(50), desc4 varchar2(50) );
create table status_level (status_level_id number, descx varchar2(50), levelid number, levels number);
insert into status values (1,'ABC_LEVEL_1','DEF_LEVEL_5','CBA_LEVEL_2','ABC_LEVEL_4');
insert into status values (2,'ABC_LEVEL_1','DEF_LEVEL_1','CBA_LEVEL_2','ABC_LEVEL_1');
insert into status values (3,'ABC_LEVEL_4','DEF_LEVEL_1','CBA_LEVEL_2','ABC_LEVEL_4');
insert into status_level values (1,'ABC_LEVEL_1',1,7);
insert into status_level values (1,'DEF_LEVEL_5',5,6);
insert into status_level values (1,'CBA_LEVEL_2',2,3);
insert into status_level values (1,'ABC_LEVEL_4',4,5);
insert into status_level values (1,'DEF_LEVEL_1',1,2);
commit;
select * from status;
select * from status_level;
select * from status, status_level where descx = desc1
union
select * from status, status_level where descx = desc2
union
select * from status, status_level where descx = desc3
union
select * from status, status_level where descx = desc4;
select status_id, max(levelid), max(levels) from (
select * from status_level, status where descx = desc1
union
select * from status_level, status where descx = desc2
union
select * from status_level, status where descx = desc3
union
select * from status_level, status where descx = desc4)
group by status_id;
Table created.
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
Commit complete.
STATUS_ID DESC1 DESC2 DESC3 DESC4
---------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- --------------------------------------------------
1 ABC_LEVEL_1 DEF_LEVEL_5 CBA_LEVEL_2 ABC_LEVEL_4
2 ABC_LEVEL_1 DEF_LEVEL_1 CBA_LEVEL_2 ABC_LEVEL_1
3 ABC_LEVEL_4 DEF_LEVEL_1 CBA_LEVEL_2 ABC_LEVEL_4
3 rows selected.
STATUS_LEVEL_ID DESCX LEVELID LEVELS
--------------- -------------------------------------------------- ---------- ----------
1 ABC_LEVEL_1 1 7
1 DEF_LEVEL_5 5 6
1 CBA_LEVEL_2 2 3
1 ABC_LEVEL_4 4 5
1 DEF_LEVEL_1 1 2
5 rows selected.
STATUS_ID DESC1 DESC2 DESC3 DESC4 STATUS_LEVEL_ID DESCX LEVELID LEVELS
---------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- --------------- -------------------------------------------------- ---------- ----------
1 ABC_LEVEL_1 DEF_LEVEL_5 CBA_LEVEL_2 ABC_LEVEL_4 1 ABC_LEVEL_1 1 7
1 ABC_LEVEL_1 DEF_LEVEL_5 CBA_LEVEL_2 ABC_LEVEL_4 1 ABC_LEVEL_4 4 5
1 ABC_LEVEL_1 DEF_LEVEL_5 CBA_LEVEL_2 ABC_LEVEL_4 1 CBA_LEVEL_2 2 3
1 ABC_LEVEL_1 DEF_LEVEL_5 CBA_LEVEL_2 ABC_LEVEL_4 1 DEF_LEVEL_5 5 6
2 ABC_LEVEL_1 DEF_LEVEL_1 CBA_LEVEL_2 ABC_LEVEL_1 1 ABC_LEVEL_1 1 7
2 ABC_LEVEL_1 DEF_LEVEL_1 CBA_LEVEL_2 ABC_LEVEL_1 1 CBA_LEVEL_2 2 3
2 ABC_LEVEL_1 DEF_LEVEL_1 CBA_LEVEL_2 ABC_LEVEL_1 1 DEF_LEVEL_1 1 2
3 ABC_LEVEL_4 DEF_LEVEL_1 CBA_LEVEL_2 ABC_LEVEL_4 1 ABC_LEVEL_4 4 5
3 ABC_LEVEL_4 DEF_LEVEL_1 CBA_LEVEL_2 ABC_LEVEL_4 1 CBA_LEVEL_2 2 3
3 ABC_LEVEL_4 DEF_LEVEL_1 CBA_LEVEL_2 ABC_LEVEL_4 1 DEF_LEVEL_1 1 2
10 rows selected.
STATUS_ID MAX(LEVELID) MAX(LEVELS)
---------- ------------ -----------
1 5 7
2 2 7
3 4 5
3 rows selected.