Pivoting and Unpivoting

Introduction

Pivoting and unpivoting are essential SQL operations that transform data structures for better insights and flexibility. These techniques are widely used in data analysis, reporting, and ETL (Extract, Transform, Load) processes. In this guide, we’ll provide examples of pivoting and unpivoting in SQL, optimized for readers interested in data transformation and database management.


What is Pivoting in SQL?

Pivoting converts rows into columns to display aggregated data in a more readable format. This is particularly useful for creating summary reports.

Example: Pivoting Sales Data

Scenario: You have a Sales table containing product sales by year.

YearProductSales
2023Product A100
2023Product B150
2024Product A200
2024Product B300

Goal: Transform the rows of years into columns.

Desired Output:

Product2023 Sales2024 Sales
Product A100200
Product B150300

SQL Query Using Conditional Aggregation:

SELECT
    Product,
    SUM(CASE WHEN Year = 2023 THEN Sales ELSE 0 END) AS "2023 Sales",
    SUM(CASE WHEN Year = 2024 THEN Sales ELSE 0 END) AS "2024 Sales"
FROM Sales
GROUP BY Product;

Benefits of Pivoting:

  • Makes data easier to read for reporting.
  • Aggregates information for specific categories.

What is Unpivoting in SQL?

Unpivoting transforms columns into rows, making the data more normalized. This is useful for preparing data for analysis tools and database operations.

Example: Unpivoting Sales Data

Scenario: You have a table summarizing sales data by product and year.

Product2023 Sales2024 Sales
Product A100200
Product B150300

Goal: Convert the columns for each year into rows.

Desired Output:

YearProductSales
2023Product A100
2023Product B150
2024Product A200
2024Product B300

SQL Query Using UNION ALL:

SELECT '2023' AS Year, Product, [2023 Sales] AS Sales
FROM SalesData
UNION ALL
SELECT '2024' AS Year, Product, [2024 Sales] AS Sales
FROM SalesData;

SQL Query Using UNPIVOT (SQL Server):

SELECT
    Year,
    Product,
    Sales
FROM (
    SELECT Product, [2023 Sales], [2024 Sales]
    FROM SalesData
) src
UNPIVOT (
    Sales FOR Year IN ([2023 Sales], [2024 Sales])
) unpvt;

Benefits of Unpivoting:

  • Simplifies data for analysis or integration with tools like Tableau or Power BI.
  • Converts wide tables into a long format, better suited for processing.

20 thoughts on “Pivoting and Unpivoting”
  1. Whats up very cool blog!! Man .. Beautiful .. Wonderful .. I will bookmark your site and take the feeds alsoKI’m glad to search out numerous useful information here within the publish, we’d like work out extra strategies on this regard, thanks for sharing. . . . . .

  2. Youre so cool! I dont suppose Ive learn something like this before. So nice to seek out any person with some authentic ideas on this subject. realy thank you for starting this up. this web site is something that’s needed on the web, someone with somewhat originality. useful job for bringing one thing new to the web!

  3. Hey, you used to write fantastic, but the last several posts have been kinda boring… I miss your super writings. Past several posts are just a little bit out of track! come on!

  4. After study a few of the blog posts on your website now, and I truly like your way of blogging. I bookmarked it to my bookmark website list and will be checking back soon. Pls check out my web site as well and let me know what you think.

  5. Thank you, I’ve just been looking for info approximately this topic for ages and yours is the greatest I’ve came upon till now. But, what about the bottom line? Are you sure in regards to the source?

  6. I’ve been exploring for a little bit for any high-quality articles or blog posts on this kind of area . Exploring in Yahoo I at last stumbled upon this site. Reading this information So i’m happy to convey that I’ve an incredibly good uncanny feeling I discovered just what I needed. I most certainly will make sure to don’t forget this site and give it a glance on a constant basis.

  7. Great weblog here! Additionally your website so much up very fast! What web host are you the use of? Can I get your associate hyperlink in your host? I wish my web site loaded up as quickly as yours lol

  8. Please let me know if you’re looking for a author for your site. You have some really great posts and I believe I would be a good asset. If you ever want to take some of the load off, I’d absolutely love to write some articles for your blog in exchange for a link back to mine. Please blast me an e-mail if interested. Cheers!

  9. Keep up the fantastic work, I read few blog posts on this site and I conceive that your website is rattling interesting and contains lots of excellent information.

Leave a Reply

Your email address will not be published. Required fields are marked *