By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
create table t(id int, seq int, total int);
✓
insert into t
select 1, 2, 1000 union all
select 1, 3, 1500 union all
select 1, 6, 5000 union all
select 2, 8, 2000 union all
select 2, 9, 3000 union all
select 3, 1, 4500;
✓
with s as (
select *, Row_Number() over(partition by id order by total) as s
from t
)
select ID,
Max(case when s = 1 then TOTAL end) as P1,
Max(case when s = 2 then TOTAL end) as P2,
Max(case when s = 3 then TOTAL end) as P3,
Max(case when s = 4 then TOTAL end) as P4,
Max(case when s = 5 then TOTAL end) as P5,
Max(case when s = 6 then TOTAL end) as P6,
Max(case when s = 7 then TOTAL end) as P7,
Max(case when s = 8 then TOTAL end) as P8,
Max(case when s = 9 then TOTAL end) as P9
from s
group by id;
id | P1 | P2 | P3 | P4 | P5 | P6 | P7 | P8 | P9 |
---|---|---|---|---|---|---|---|---|---|
1 | 1000 | 1500 | 5000 | null | null | null | null | null | null |
2 | 2000 | 3000 | null | null | null | null | null | null | null |
3 | 4500 | null | null | null | null | null | null | null | null |