When dealing with SQL databases, transitioning rows to columns can vastly improve data presentation and simplify analysis. This process, known as pivoting, is particularly useful when working with datasets that have repeated measures or when you need to compare data from multiple categories side-by-side. This article guides you through pivoting rows to columns using the PIVOT and CONDITIONAL AGGREGATE functions in SQL, with a focus on handling multiple categories.

Before delving into the pivot process, let's consider a scenario where we have sales data from different regions (West, East, South, North) over three years. The data is structured with one row per region per year. Our goal is to consolidate this data into a single row per year, with columns for each region's sales.

Pivoting using PIVOT Function
Microsoft SQL Server's PIVOT function is a straightforward way to transform rows into columns. It's ideal for aggregating and presenting data in a tabular format, making it perfect for our scenario.

The PIVOT function's primary argument is a column expression that you wish to transform into columns. The UNPIVOT function, its counterpart, does the oppositeβit converts columns into rows.
Preparing Data: Creating a Sample Table

The first step in pivoting involves ensuring your data is structured correctly. Let's create a sample table named 'Sales' with the following statement:
```sql CREATE TABLE Sales ( Region VARCHAR(10), Year INT, Sales INT ); ```
Insert some sample data:
```sql INSERT INTO Sales (Region, Year, Sales) VALUES ('West', 2020, 50000), ('East', 2020, 70000), ('South', 2020, 90000), ('North', 2020, 60000), ('West', 2021, 55000), ('East', 2021, 65000), ('South', 2021, 75000), ('North', 2021, 58000), ('West', 2022, 60000), ('East', 2022, 78000), ('South', 2022, 95000), ('North', 2022, 62000); ```
Using the PIVOT Function

With our data prepared, let's pivot the 'Sales' table using the PIVOT function. Here's how:
```sql SELECT Year, [West], [East], [South], [North] FROM (SELECT Region, Year, Sales FROM Sales) AS PivotTable PIVOT (SUM(Sales) FOR Region IN ([West], [East], [South], [North])) AS PivotResult; ```
The PIVOT function takes the 'Sales' column from our subquery and transforms it into pivot columns based on the 'Region' column. The 'IN' clause lists the column names to create in the result set.
Pivoting using Conditional Aggregate Functions

MySQL and PostgreSQL don't have a PIVOT function, but you can achieve similar results using conditional aggregation. The CASE expression is crucial in this process.
Let's use conditional aggregation to pivot the same 'Sales' table from our SQL Server example.



















Using SUM and CASE in MySQL
In MySQL, you can use the SUM function along with a CASE statement to create pivot columns. Here's how:
```sql SELECT Year, SUM(CASE Region WHEN 'West' THEN Sales ELSE NULL END) AS `West`, SUM(CASE Region WHEN 'East' THEN Sales ELSE NULL END) AS `East`, SUM(CASE Region WHEN 'South' THEN Sales ELSE NULL END) AS `South`, SUM(CASE Region WHEN 'North' THEN Sales ELSE NULL END) AS `North` FROM Sales GROUP BY Year; ```
Using SUM and CASE in PostgreSQL
PostgreSQL's approach is similar to MySQL, with slight syntax differences:
```sql SELECT Year, SUM(CASE Region WHEN 'West' THEN Sales ELSE NULL END) "West", SUM(CASE Region WHEN 'East' THEN Sales ELSE NULL END) "East", SUM(CASE Region WHEN 'South' THEN Sales ELSE NULL END) "South", SUM(CASE Region WHEN 'North' THEN Sales ELSE NULL END) "North" FROM Sales GROUP BY Year; ```
Remember to wrap column names in backticks (`` ` ``) when there's a space, as in MySQL, or double quotes (") in PostgreSQL.
The final step in your data transformation journey is to understand and practice these techniques. Pivoting can take your data analysis to the next level, making it more accessible and insightful. Happy coding!