Skip to content
Advertisement

How to transpose row to columns in Oracle as shown below using Unpivot?

Source Table

Cost Category Cost Category Type Q1-2020 Q2-2020 Q3-2020 Q4-2020
Employee Impacted Period Cost 10 20 0 4000

Achieved Result

Cost Category Cost Category Type Quarter Year Value
Employee Impacted Period Cost Q1-2020 10
Employee Impacted Period Cost Q2-2020 20
Employee Impacted Period Cost Q3-2020 0
Employee Impacted Period Cost Q4-2020 4000

Desired Result

Cost Category Cost Category Type Quarter Year Quarter Year Value
Employee Impacted Period Cost Q1-2020 Q1 2020 10
Employee Impacted Period Cost Q2-2020 Q2 2020 20
Employee Impacted Period Cost Q3-2020 Q3 2020 0
Employee Impacted Period Cost Q4-2020 Q4 2020 4000

I have been able to achieve table 2 above using below query but not sure how to add quarter and year column as shown in table 3

Advertisement

Answer

Use yet another CTE. Your current “solution” is temp CTE in my query; then it is a simple task to extract quarter and year.

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