• Javascript
  • Python
  • Go

Examples of PIVOTing String Data in SQL Server

When working with data in SQL Server, it is common to come across scenarios where the data needs to be transformed or pivoted in order to ma...

When working with data in SQL Server, it is common to come across scenarios where the data needs to be transformed or pivoted in order to make it more meaningful and usable. One such scenario is when dealing with string data, where the values need to be pivoted in order to analyze and compare them. In this article, we will explore some examples of how to pivot string data in SQL Server using the PIVOT function.

But first, let's understand what pivoting means in the context of SQL Server. Pivoting is the process of transforming rows of data into columns, in order to better organize and analyze the data. It is a powerful technique that allows for easier data analysis and reporting.

Now, let's dive into some examples of PIVOTing string data in SQL Server:

Example 1: Pivoting a single column of values

Suppose we have a table called "Sales" with the following data:

| Product | Month | Sales |

|---------|-------|-------|

| A | Jan | 100 |

| A | Feb | 150 |

| B | Jan | 200 |

| B | Feb | 250 |

In order to pivot the data by month and display the sales for each product, we can use the PIVOT function as follows:

```sql

SELECT Product, [Jan], [Feb]

FROM

(

SELECT Product, Month, Sales

FROM Sales

) AS SalesData

PIVOT

(

SUM(Sales) FOR Month IN ([Jan], [Feb])

) AS PivotTable

```

This will give us the following result:

| Product | Jan | Feb |

|---------|-----|-----|

| A | 100 | 150 |

| B | 200 | 250 |

Example 2: Pivoting multiple columns of values

In some cases, we may need to pivot multiple columns of string values. Let's say we have a table called "Employee" with the following data:

| EmployeeID | Name | Department | Gender |

|------------|----------|------------|--------|

| 1 | John Doe | Sales | Male |

| 2 | Jane Doe | Marketing | Female |

| 3 | Bob Smith| HR | Male |

| 4 | Sarah Lee| Finance | Female |

To pivot the data by department and gender, we can use the PIVOT function as follows:

```sql

SELECT Name, [Sales_Male], [Sales_Female], [Marketing_Male], [Marketing_Female], [HR_Male], [HR_Female], [Finance_Male], [Finance_Female]

FROM

(

SELECT EmployeeID, Name, Department, Gender

FROM Employee

) AS EmployeeData

PIVOT

(

COUNT(EmployeeID) FOR Department IN ([Sales], [Marketing], [HR], [Finance])

) AS PivotTable

```

This will give us the following result:

| Name | Sales_Male | Sales_Female | Marketing_Male | Marketing_Female | HR_Male | HR_Female | Finance_Male | Finance_Female |

|----------|------------|--------------|----------------|------------------|---------|-----------|--------------|----------------|

| John Doe | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |

| Jane Doe | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |

| Bob Smith| 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 |

| Sarah Lee| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |

Example 3: Pivoting string values to different columns

In some cases, we may need to pivot string values to different columns based on certain conditions. Let's say we have a table called "Orders" with the following data:

| OrderID | Product | Quantity | Status |

|---------|---------|----------|----------|

| 1 | A | 5 | Shipped |

| 2 | B | 3 | Pending |

| 3 | A | 2 | Shipped |

| 4 | C | 4 | Delivered|

To pivot the data by status and display the quantity for each product, we can use the PIVOT function as follows:

```sql

SELECT Product, [Shipped], [Pending], [Delivered]

FROM

(

SELECT Product, Quantity, Status

FROM Orders

) AS OrdersData

PIVOT

(

SUM(Quantity) FOR Status IN ([Shipped], [Pending], [Delivered])

) AS PivotTable

```

This will give us the following result:

| Product | Shipped | Pending | Delivered |

|---------|---------|---------|-----------|

| A | 7 | 0 | 0 |

| B | 0 | 3 | 0 |

| C | 0 | 0 | 4 |

In conclusion, the PIVOT function in SQL Server is a powerful tool for transforming and analyzing string data. It allows for easy pivoting of data into columns, making it easier to understand and analyze. By understanding these examples, you can apply the PIVOT function in different scenarios and make the most out of your string data.

Related Articles