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!

Dynamic pivot

unknown-879931Oct 17 2014 — edited Oct 17 2014


Hi Experts,

I am trying to do dynamic pivot query. I know how to transpose a query to pivot. But I am just wondering can I do pivot.

For example.

A classic pivot:

>>select * from sec_q;

the column names respectively are:

invoiceid  invoice_amount invoice_row_num

01032925 196,28 1

01048083 152,57 1

01048083 98,87 2

01053119 188,5 1

01070391 358,84 1

01078898 165,78 1

01081126 157,57 1

01085899 87,19 1

01085899 1,31 2

01085899 164,82 3

However, I don't know the max invoice_row_num value because it can decrease dynamicly

I want the following output

accountnum      invoice_num_1   invoice_num_2  invoice_num_3......  invoice_num_max

01085899                87,19                    1,31                164,82                    0

01081126                157,57                     0                         0                       0

As far as I know the following cannot meet my expectations

SELECT * FROM sec_q

pivot

(max(invoiceamount) for (row_num) in ('1', '2', '3', '4', '5'  *BECAUSE THIS VALUE CHANGE DYNAMICLY *   ));

So, suppose that my table like below

sec_q

(

accountnumnumber,

invoice_amount number,

invoice_row_num number

)

each accountnum does not dublicate, and all invoice_amoun tranpose and the column headers should be invoice_row value

Does it possible?

Thanks in advance

This post has been answered by Frank Kulash on Oct 17 2014
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 14 2014
Added on Oct 17 2014
2 comments
813 views