Continuation of PL/SQL: Some helpful SQL queries -1
Refer:
PIVOT and UNPIVOT Operators in Oracle Database 11g Release
Pivot and Unpivot
#7. How to convert Multiple Columns into Rows (Unpivot multiple columns).
Source Data
Source Data
Expected output
withtas(select'1001'emp_no,'Anoop'emp_first_name,'Johny'emp_last_name ,'Developer'job_role,'Senior Developer'new_job_role ,'150000'salary ,'175000'new_salary ,'Australia'country ,'Australia'new_countryfromdualunionselect'1002'emp_no,'Sajeer'emp_first_name,'Omar'emp_last_name ,'Senior Developer'job_role,'Developer'new_job_role ,'105000'salary ,'100000'new_salary ,'Australia'country ,'Australia'new_countryfromdual)select(casewhenrow_number()over(partitionbyemp_noorder byemp_no ) = 1thenemp_noend) emp_no ,(case whenrow_number()over(partitionbyemp_noorder byemp_no ) = 1thenemp_first_nameend) emp_first_name ,(case whenrow_number()over(partition byemp_last_nameorder byemp_no ) = 1thenemp_last_nameend) emp_last_name ,col detail ,val old_value ,new_val new_valuefromtunpivot include nulls((val,new_val)for col in((job_role,new_job_role)as'Job Role',(salary,new_salary)as'Salary',(country,new_country)as'Country'));
Refer:
PIVOT and UNPIVOT Operators in Oracle Database 11g Release
Pivot and Unpivot
Feel free to point out if anything is missing/wrong in this blog.
No comments:
Post a Comment