Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

rows to fixed number of columns in oracle query

SmileMar 5 2019 — edited Mar 11 2019

Hi ,

I always need to  show the amount in 4  columns only .

For ex :

If there are 6 rows in table ,  1st row should have 4 columns and 2nd row should have 2 columns

If there are 9 rows in table ,  1st row should have 4 columns and 2nd row should have 4 columns and 3rd row should have 1 column.

with t as
( select 100 s_id , 1276 amount from dual union all
select 100 s_id , 5678 amount from dual union all
select 100 s_id , 2324 amount from dual union all
select 100 s_id , 4532 amount from dual union all
select 100 s_id , 1209 amount from dual union all
select 100 s_id , 8901 amount from dual )

select s_id ,amount from t

expected output

--In Row 1
amount_1  amount_2  amount_3  amount_4
1276      5678      2324      4532
--In Row 2
1209      8901      0          0   

This post has been answered by Solomon Yakobson on Mar 5 2019
Jump to Answer
Comments
Post Details
Added on Mar 5 2019
4 comments
630 views