What is Pivoting in SQL?
Pivoting is a data transformation technique in SQL where rows are converted into columns. This transformation allows you to display data in a tabular format that’s more suitable for analysis, reporting, or dashboards. The operation involves taking unique values from a column and turning them into column headers, with aggregated data filling the cells of the new table.
Why Use Pivoting?
- Readability
Pivoting makes raw data easier to interpret. For example, sales data for multiple years can be transformed into a table where each year’s sales appear as a separate column, making comparisons simple. - Aggregation
Pivoting allows for aggregation of data (e.g., totals, averages) within specific groups, making it invaluable for generating summary reports. - Comparison
By displaying data side-by-side, pivoting makes it easy to compare values across categories, time periods, or groups.
Example Scenario:
Suppose you manage sales data with columns for years, regions, and sales amounts. Instead of having multiple rows for each year, you can pivot the data to display years as columns and regions as rows.
How Pivoting Works in SQL
- Identify the Columns to Pivot
Choose a column whose unique values will become new column headers (e.g., “Year” in sales data). - Apply Aggregation
Use an aggregate function likeSUM
,COUNT
, orAVG
to compute the values for the pivoted columns. - Group the Data
UseGROUP BY
to organize data by the remaining columns (e.g., “Region” in sales data).
Pivoting in Different Relational Databases
1. SQL Server
SQL Server provides the PIVOT
operator, which simplifies the process of creating pivoted tables. The PIVOT
operator automates column transformation and aggregation.
Example: Sales Data
Input Table:
Year | Region | Sales |
---|---|---|
2023 | East | 1200 |
2023 | West | 1500 |
2024 | East | 1800 |
2024 | West | 2000 |
Desired Output:
Region | 2023 | 2024 |
---|---|---|
East | 1200 | 1800 |
West | 1500 | 2000 |
Query Using PIVOT
:
SELECT *
FROM (
SELECT Year, Region, Sales
FROM Sales
) src
PIVOT (
SUM(Sales)
FOR Year IN ([2023], [2024])
) pvt;
Explanation:
- The subquery (
src
) selects the raw data. - The
PIVOT
operator creates new columns (2023
and2024
) and applies theSUM
aggregation to fill the values.
2. PostgreSQL
PostgreSQL does not have a built-in PIVOT
operator. Instead, it uses conditional aggregation to pivot data.
Example: Sales Data
Query Using Conditional Aggregation:
SELECT
Region,
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 Region;
Explanation:
- The
CASE
statement checks the value of theYear
column. - The
SUM
function aggregates sales for each year into separate columns. - The
GROUP BY
clause organizes the results byRegion
.
3. MySQL
Similar to PostgreSQL, MySQL also relies on conditional aggregation.
Query:
SELECT
Region,
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 Region;
The structure and logic are the same as in PostgreSQL. MySQL uses the CASE
statement to implement pivoting.
4. Oracle
Oracle provides a built-in PIVOT
operator, similar to SQL Server.
Query Using PIVOT
:
SELECT *
FROM (
SELECT Year, Region, Sales
FROM Sales
) src
PIVOT (
SUM(Sales)
FOR Year IN (2023 AS "2023 Sales", 2024 AS "2024 Sales")
);
Explanation:
- The subquery (
src
) provides the source data. - The
PIVOT
operator transforms the rows into columns (2023
and2024
), withSUM
used to aggregate the sales data.
5. SQLite
SQLite does not support a PIVOT
operator, so you must use conditional aggregation.
Query:
SELECT
Region,
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 Region;
Dynamic Pivoting
Dynamic pivoting is used when the column names for the pivoted table are not known in advance. This is common when new data (e.g., years or categories) is added dynamically.
Dynamic Pivot in SQL Server
DECLARE @columns NVARCHAR(MAX);
DECLARE @query NVARCHAR(MAX);
-- Generate column list dynamically
SELECT @columns = STRING_AGG(QUOTENAME(Year), ',')
FROM (SELECT DISTINCT Year FROM Sales) AS Years;
-- Create dynamic SQL query
SET @query = '
SELECT *
FROM (
SELECT Year, Region, Sales
FROM Sales
) src
PIVOT (
SUM(Sales)
FOR Year IN (' + @columns + ')
) pvt;';
-- Execute the query
EXEC sp_executesql @query;
Use Cases for Pivoting
- Financial Reporting
Aggregate revenues or expenses by month or quarter for each department. - Product Sales Reports
Summarize product sales across regions or years. - Employee Performance
Show employee KPIs across different periods or projects. - Inventory Analysis
Display stock levels by category and warehouse location.
Comparison of Pivoting Methods
Database | Supports PIVOT Operator? | Alternative Method |
---|---|---|
SQL Server | Yes | Conditional Aggregation |
PostgreSQL | No | Conditional Aggregation |
MySQL | No | Conditional Aggregation |
Oracle | Yes | Conditional Aggregation (Optional) |
SQLite | No | Conditional Aggregation |
I have learn some good stuff here. Certainly worth bookmarking for revisiting. I wonder how a lot effort you put to create this kind of fantastic informative website.
naturally like your web site but you need to test the spelling on several of your posts. Several of them are rife with spelling problems and I in finding it very troublesome to inform the reality however I will definitely come back again.
What’s Taking place i’m new to this, I stumbled upon this I have found It absolutely useful and it has aided me out loads. I hope to give a contribution & help other users like its helped me. Good job.
Thank you for the auspicious writeup. It in fact used to be a entertainment account it. Glance complicated to far delivered agreeable from you! However, how could we keep in touch?
It?¦s really a cool and helpful piece of info. I?¦m happy that you shared this useful info with us. Please stay us up to date like this. Thanks for sharing.
You really make it seem so easy with your presentation but I find this topic to be actually something that I believe I would never understand. It kind of feels too complicated and extremely large for me. I’m having a look ahead on your next put up, I will try to get the grasp of it!
Pretty! This was a really wonderful post. Thank you for your provided information.
Normally I do not read post on blogs, but I would like to say that this write-up very forced me to try and do it! Your writing style has been surprised me. Thanks, very nice post.
Outstanding post however , I was wondering if you could write a litte more on this subject? I’d be very thankful if you could elaborate a little bit further. Kudos!
Good article and right to the point. I don’t know if this is actually the best place to ask but do you people have any thoughts on where to get some professional writers? Thank you 🙂
you have a great blog here! would you like to make some invite posts on my blog?
Saved as a favorite, I really like your blog!
Great write-up, I am normal visitor of one’s website, maintain up the excellent operate, and It is going to be a regular visitor for a long time.
Great post. I was checking continuously this blog and I am impressed! Extremely useful information specially the last part 🙂 I care for such information much. I was looking for this certain info for a very long time. Thank you and best of luck.