Categories: SQL Tutorials

How to Pivot Queries in SQL with Examples?

In my previous article we have explained about Pivot in SQL with examples. In SQL, pivoting refers to the process of transforming rows into columns or columns into rows. Pivoting queries can be useful when you need to present your data in a different format than what is stored in the database. This article will explain how to pivot queries in SQL with examples.

Example :

Let’s start with an example dataset. Consider a table named “Sales” that contains the following data:

DateProductSales
2022-01-01A100
2022-01-02A150
2022-01-01B200
2022-01-02B250

In this example, we have sales data for two products, A and B, for two different dates.

Pivoting Rows into Columns

To pivot rows into columns, we use the “PIVOT” operator. The “PIVOT” operator takes the values from a specified column and turns them into column headers. Here’s an example:

SELECT *
FROM Sales
PIVOT (
    SUM(Sales)
    FOR Product IN ([A], [B])
) AS P

In this query, we are pivoting the “Product” column into column headers. We are also calculating the sum of sales for each product using the “SUM” function. The resulting table looks like this:

DateAB
2022-01-01100200
2022-01-02150250

Now we have two columns, “A” and “B”, that represent the sales for each product.

Pivoting Columns into Rows

To pivot columns into rows, we use the “UNPIVOT” operator. The “UNPIVOT” operator takes the column headers and turns them into rows. Here’s an example:

SELECT *
FROM (
    SELECT Date, A, B
    FROM Sales
) AS P
UNPIVOT (
    Sales FOR Product IN (A, B)
) AS U

In this query, we are unpivoting the “A” and “B” columns into rows using the “UNPIVOT” operator. We are also selecting the “Date” column from the “Sales” table. The resulting table looks like this:

DateProductSales
2022-01-01A100
2022-01-02A150
2022-01-01B200
2022-01-02B250

Now we have three columns, “Date”, “Product”, and “Sales”, with each row representing a single sale.

Conclusion

Pivot queries in SQL can be a useful tool for transforming your data into a different format. Whether you’re pivoting rows into columns or columns into rows, the “PIVOT” and “UNPIVOT” operators can help you achieve your desired results.

Amit S

Oracle Consultant with vast experience in Oracle BI and PL/SQL Development. Amiet is the admin head of this website who contributes by preparing tutorials and articles related to database technologies. He is responsible to manage the content and front-end of the website.

Recent Posts

What is Root Cause Analysis (RCA) With real examples

In my previous article I have given details about application support engineer day to day…

4 weeks ago

Application Support Engineer Day to day responsibilities

In my previous articles I have given the roles and responsibilities of L1,L2 and L3…

1 month ago

What is mean by SLA ( Service Level Agreement) with Examples?

In my previous articles i have given the hierarchy of production support in real company…

1 month ago

What is Production support Hierarchy in organization?

In this article i would like to provide information about production support organization structure or…

1 month ago

What are roles and responsibilities for L3 Support Engineer?

In my previous article I have given roles for L1 and L2 support engineer with…

1 month ago

What are roles and responsibilities of L2 Engineer?

I have started this new series of how to become application support engineer. This article…

1 month ago