Skip to content
Advertisement

Oracle SQL Pivot with Conditions convert rows to columns

I have the a table ITEM_SF with the following data

I want to convert the data into Columns with a few conditions:

My desired result is to show Charges, Discounts , Amount Paid and Tax Charges include (BookFees, HealthFees,RegistrationFees,SSCFees,TuitionFees,GeneralFees)

Charges

, Discount is Sum of ITEM_AMT where ITEM_TYPE_CD ='W' FeesPaid is Sum of ITEM_AMT where ITEM_TYPE_CD ='P' VAT is Sum of ITEM_AMT where ACCOUNT_NBR=VAT001 and ITEM_TYPE_CD ='C' (Aditionally VAT should always be Charges*0.05)

I tried using Pivot but could not get all the columns mentioned

Advertisement

Answer

You can use conditional aggregation, which means using a case expression to define which values should be summed up in each column:

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement