Вы можете использовать correlated subqueries
и row_number()
окно аналитической функции вместе
with table1(row_id, var, var_val) as
(
select 1,'Test 1','123' from dual union all
select 1,'Test 2','456' from dual union all
select 1,'Test 3','789' from dual union all
select 1,'Test 4','1234' from dual union all
select 2,'Test 1','665t' from dual union all
select 2,'Test 2','dsfs' from dual union all
select 2,'Test 3','df' from dual union all
select 2,'Test 4','sfd' from dual union all
select 3,'Test 1','sfs' from dual union all
select 3,'Test 2','sf' from dual union all
select 3,'Test 3','sdfs' from dual union all
select 3,'Test 4','sfsd' from dual
), t2 as
(
select t.*, row_number() over (partition by var order by row_id) as rn
from table1 t
)
select distinct row_id,
(select var_val
from table1 t2
where t2.var = 'Test 1'
and t2.row_id = rn) as var1,
(select var_val
from table1 t2
where t2.var = 'Test 2'
and t2.row_id = rn) as var2
from t2
order by row_id
ROW_ID VAR1 VAR2
------ ---- ----
1 123 456
2 665t dsfs
3 sfs sf
Демо