Приветы!
Имеем табл.
id | name | val | 1 | n1 | 12 | 2 | n2 | 12 | 3 | n3 | 13 | 1 | n1 | 15 | 5 | n5 | 10 | 3 | n3 | 5 | 2 | n2 | 22 | 2 | n2 | 25 | 1 | n1 | 24 |
|
Получ. данные запросом
with t(id,name,val) as
(select 1,'n1',12 from dual union all
select 2,'n2',12 from dual union all
select 3,'n3',13 from dual union all
select 1,'n1',15 from dual union all
select 5,'n5',10 from dual union all
select 3,'n3',5 from dual union all
select 2,'n2',22 from dual union all
select 2,'n2',25 from dual union all
select 1,'n1',24 from dual
)
select id,name
,max(case when rn=1 then val else null end) v1
,max(case when rn=2 then val else null end) v2
,max(case when rn=3 then val else null end) v3
from (
select id,name,val,row_number() over(partition by id order by val) rn
from t)
group by id,name
order by 3
id | name | v1 | v2 | v3 | 3 | n3 | 5 | 13 | | 5 | n5 | 10 | | | 1 | n1 | 12 | 15 | 24 | 2 | n2 | 12 | 22 | 25 |
|
Интересует альтернативное решение, чтобы не писать для каждого (возможного) case ?