EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials Oracle Tutorial PIVOT in Oracle
Secondary Sidebar
Oracle Tutorial
  • Advanced
    • Timestamp to Date in Oracle
    • Oracle Golden Gate
    • Oracle Virtual Machine
    • Oracle Describe Table
    • Oracle Clauses
    • Oracle Having Clause
    • Oracle?Primavera
    • Oracle FOREIGN Key
    • PIVOT in Oracle
    • Oracle Alter Table
    • Oracle Queries
    • Oracle Views
    • Oracle Window Functions
    • Oracle String Functions
    • Oracle Date Functions
    • Oracle Analytic Functions
    • Oracle Aggregate Functions
    • Select in Oracle
    • INSERT in Oracle
    • DISTINCT in Oracle
    • Function in Oracle
    • Oracle GROUP_CONCAT
    • Oracle INSTR()
    • Oracle CONVERT
    • Oracle LENGTH()
    • Oracle EXISTS
    • Oracle REPLACE()
    • Oracle MERGE
    • Oracle LEAD()
    • Oracle EXTRACT()
    • Oracle LISTAGG()
    • Oracle SYS_CONTEXT()
    • Oracle COALESCE
    • Oracle NVL()
    • Oracle SYSDATE()
    • Oracle?Date Format
    • Oracle SYS_GUID()
    • Oracle WILDCARDS
    • Oracle Synonyms
    • Oracle Subquery
    • BETWEEN in Oracle
    • FETCH in Oracle
    • Oracle Index
    • Oracle Function-based Index
    • Oracle UNIQUE Index
    • Oracle Bitmap Index
    • Oracle Column
    • Oracle Triggers
    • Oracle Procedures
    • Sample Database for Oracle
    • Oracle LIKE Operator
    • ORDER BY in Oracle
    • Oracle ORDER BY DESC
    • GROUP BY in Oracle
    • Oracle GROUP BY HAVING
    • Oracle Aliases
    • Table in Oracle
    • Oracle Temporary Table
    • Oracle? Table Partition
    • Oracle rename table
    • Oracle CTE
    • Cursor in Oracle
    • Oracle LOCK TABLE
    • Oracle Tablespace
    • Oracle CARDINALITY
    • Oracle REGEXP
    • Oracle REGEXP_REPLACE
    • Oracle to_date
    • JSON in Oracle
    • Oracle COMMIT
    • Oracle GRANT
    • Oracle MD5
    • Oracle ROLLBACK
    • Oracle Users
    • Oracle TIMESTAMP
    • IF THEN ELSE in Oracle
    • Oracle While Loop
    • Oracle Clone Database
    • Oracle Backup Database
    • Oracle? XML
    • Oracle XMLAGG
    • Oracle XMLTABLE
    • Oracle Performance Tuning
    • Oracle B Tree Index
    • Oracle fusion
    • Oracle ebs
    • Oracle GRC
    • Oracle ERP
    • Oracle ASM
    • Oracle Cloud
    • Oracle HCM Cloud
    • Oracle Integration Cloud
    • Oracle Jinitiator
    • Oracle pathfinder
    • Oracle VirtualBox
    • Oracle Weblogic Server
    • Oracle decode
    • Oracle Exadata
    • Oracle ZFS
    • Oracle? utilities
    • JDBC Driver for Oracle
    • Oracle? DBA Versions
    • Oracle DBA Salary
  • Basic
    • Oracle Marketing Cloud
    • What is Oracle?
    • Career in Oracle
    • How to Install Oracle
    • Oracle Versions
    • What Is Oracle Database
    • Oracle Data Warehousing
    • Oracle Warehouse Builder
    • Career In Oracle Database Administrator
    • Career In Oracle DBA
    • What is Oracle RAC
    • Oracle DBA
    • Oracle? Vanderbilt
    • What is RMAN Oracle
    • Oracle Database Administration
    • Oracle Operators
    • Oracle Constraints
    • Oracle number
    • Oracle Data Types
    • Oracle UNIQUE Constraint
    • Oracle Check Constraint
  • Joins
    • Joins in Oracle
    • Inner Join in Oracle
    • Oracle Cross Join
    • Left Join in Oracle
    • OUTER Join in Oracle
    • Oracle Full Outer Join
    • Natural Join in Oracle
    • Oracle Self Join
    • Oracle hash join
    • Oracle? Update with Join
  • Oracle SET Operators
    • UNION in Oracle
    • Oracle UNION ALL
    • INTERSECT in Oracle
    • MINUS in Oracle
  • Interview Questions
    • Oracle Interview Questions
    • Oracle Apps Interview Questions
    • Oracle Apps Technical Interview Questions
    • Oracle Database Interview Questions
    • Oracle Forms Interview Questions
    • Oracle PL/SQL Interview Questions
    • Oracle RAC Interview Questions
    • Oracle SOA Interview Questions

Related Courses

Oracle Course Training

Oracle DBA Certification Course

MongoDB Certification Training

PIVOT in Oracle

By Priya PedamkarPriya Pedamkar

PIVOT in Oracle

What is an Oracle PIVOT Clause?

An Oracle PIVOTclauseis used to get a result in a matrix form. It allows us to write a query in cross-tabulation form. It also enables us to transpose rows into columns. It is introduced in Oracle 11g version and it supports 11g or later version. This is a very useful function for transposing rows into columns and aggregating data in the process. It is also called a CROSSTAB query. Multiple columns can be used in the PIVOT clause. If rows need to be converted into columns to split out a specific set of data points then the PIVOT clause can be used.

Syntax and Parameters

The syntax of the pivot in oracle:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,112 ratings)

Syntax:

SELECT Columns FROM Table_Name
PIVOT [XML] (
Pivot_Clause
Pivot_For_Clause
Pivot_In_Clause
)   WHERE Conditions

Parameter:

  • Columns: Columns that needs to be displayed or fetched.
  • Table_Name: Table that can be used for data.
  • XML: It is used to convert pivoted data into xml. It is an optional keyword.
  • Pivot_Clause: It uses Oracle aggregate function on the column’s data to fill the pivoted column accordingly. Oracle aggregate functions are SUM,COUNT,MIN,MAX or AVG.
  • Pivot_For_Clause: In this clause, specify column will be grouped or pivoted.
  • Pivot_In_Clause: It is used to define a filter for the specified column in Pivot_For_Clause.
  • WHERE: This clause is used to filter the data. It’s optional.
Note: Subquery can be used instead of Table_Name. It’s not mandatory but it depends on the requirement. ORDER BY clause can be used for sorting the result but it could be the last clause. Subquery can be used for Pivot_In_Clause to get the column name.

How does PIVOT Clause Work in Oracle?

ThePIVOTclause is a key technique in many data warehouse applications. Its main functionality is to transpose rows into the column. It transforms multiple input rows into columns, aggregating data as part of the rotation process.

Implementing EXTRACT( ) Function

In this section we’ll see the implementation of Oracle PIVOT Clause and its behavior. For that, we will use the below sample table (Employee) with 14records to understand the Oracle PIVOT Clause behavior.

SELECT * FROM Employee;

Output:

PIVOT in Oracle-1.1

1. Oracle PIVOT Clause

SELECT * FROM (SELECT Name, Designation, Deptnumber, Salary FROM                  Employee) PIVOT (SUM (Salary) FORDeptnumber IN (10,20,30));

Output:

PIVOT in Oracle-1.2

In the above example, In the PIVOTclause SUM () function performs a summation of salary forDeptnumbrwhich is declared using Pivot FOR clause. In Pivot IN clause, department numbers have been declared which is pointing to the FOR clause Deptnumbr column and converts Deptnumbr column values into column names and displays the sum of salary Deptnumber wise.

2. PIVOT Column Aliasing or Alias Name

Pivot column aliasing is nothing but to provide a temporary name to the column or values.

  • In the Pivot_Clause, one or more columns can have an alias name.
  • In the Pivot_In_Clause, one or more values can have an alias name.
  • The final column name (after aliasing) will be:

Pivot_In_Clause Alias || ‘_’ || Pivot_Clause Alias
SELECT * FROM (SELECT Designation, Deptnumber, Salary FROM Employee)                       PIVOT (SUM (Salary) Dept_Sal FOR Deptnumber IN (10, 20, 30));

Output:

PIVOT in Oracle-1.3

In the above example, Dept_Sal is an Alias name of Pivot_Clause (SUM (Salary)). The alias name gets concatenated or added with Pivot_In_Clause using underscore ‘_’.

3. Multiple Columns in PIVOT Clause

In PIVOT clause, multiple columns can be used in Pivot_Clause.

SELECT * FROM (SELECT Designation, Deptnumber, Salary FROM Employee)          PIVOT (SUM (Salary) Dept_Sal, COUNT(DeptNumber) Dept_Count FOR                             Deptnumber IN (10, 20));

Output:

Output-1.4

In the above PIVOTClause, there are two columns (Salary & DeptNumber) are being used for aggregation in Pivot_Clause. The output showing summation of salary department wise and count of the employee Deptnumber wise who falls in the same department with the same designation and returns the result

4. Sub Query in Oracle PIVOT

SELECT * FROM (SELECT Designation, Deptnumber, Salary FROM Employee)    PIVOT (SUM(Salary) Dept_Sal, COUNT(DeptNumber) Dept_Count FOR                                   Deptnumber IN (select Deptnumber from employee));

Output:

Output-1.5

The above PIVOT Clause example returns an error “missing expression”.WHY?

Because that subquery return all department value means duplicate values that’s why in Pivot_In_Clause subquery is not allowed or not acceptable. If you declare, it will return an error.

But Sub Query can be used in Pivot_In_Clause, see the example below.

SELECT * FROM (SELECT Designation, Deptnumber, Salary FROM Employee)    PIVOT (SUM(Salary) Dept_SalFOR Deptnumber IN (select Deptno FROMEmp));

Output:

Output-1.6

In the above Pivot clause, used a subquery to populate department number in Pivot_In_Clause and it returns an output without any error. Because in the above example XML keyword is used which converts subquery returns values in a single XML string column.

Note: Subquery must return a list of unique value(s). In Subquery Group by clause won’t work for unique value instead of XML keyword.

Conclusion

Oracle PIVOTclause is a very useful clause that adds more practical functionality in the field of data analysis or crosstab reporting. Pivot clause reduced writing of convoluted non-intuitive or lengthy code. It is a very efficient way to transpose the data.

Recommended Articles

This is a guide to PIVOT in Oracle. Here we also discuss the introduction and how does pivot clause work in oracle? along with different examples and its code implementation. you may also have a look at the following articles to learn more –

  1. Oracle LOCK TABLE
  2. IF THEN ELSE in Oracle
  3. Oracle REGEXP_REPLACE
  4. Oracle CARDINALITY
Popular Course in this category
Oracle Training (14 Courses, 8+ Projects)
  14 Online Courses |  8 Hands-on Projects |  120+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Oracle DBA Database Management System Training (2 Courses)4.9
All in One Financial Analyst Bundle- 250+ Courses, 40+ Projects4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more