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!

Pivot-like query

ZudarOct 25 2018 — edited Oct 25 2018

Hi all,

I could really use some help solving the following. I have the following (simplified) table:

ID
LINEITEMAMOUNTREVENUE
3928471A2200
3928472B150
3928473D3400
3928481C120
3928482E160
...............

What I need is a select-statement on the upper table to result in the following:

ID
A_PRESENTB_PRESENTC_PRESENTD_PRESENTE_PRESENTA_AMOUNTB_AMOUNTC_AMOUNTD_AMOUNTE_AMOUNTA_REVENUEB_REVENUEC_REVENUED_REVENUEE_REVENUE
39284711010210302005004000
39284800101001010020060
................................................

So what's happening is that we take every unique ID from the source table and then connect this to a set of columns that tells us more about the items that were included in this unique ID. So first, for all possible ITEMs, we give a binary value for whether the item is present or not. Second and last, we store the AMOUNTs and REVENUEs again for each individual item. *I want the items (A to E) to be derived from the source table and not be predefined. I was trying to make this work using the PIVOT function, but right now I'm not sure that's the right direction. What do you think?

*EDIT: I can find a way to have the set of ITEMs predefined before running the query. It's not necessary to derive the ITEMS from the source table.

Thanks for any help on this, appreciate it!

Comments
Post Details
Added on Oct 25 2018
7 comments
892 views