SQL PIVOT

In SQL Pivot is one of the most useful Operator to convert the Row values into Column names or simply say, Rotating table. While rotating the table, or SQL Pivot Table remaining column values must be involved in Grouping or Aggregation. We use the below query to convert rows into the column using Pivot Table in SQL server.

USE AdventureWorks2014
GO
SELECT PROD.Name,
       YEAR(OrdHead.OrderDate) AS [Order Year],
       SUM(Details.OrderQty) AS [Order Quantity]
  FROM Sales.SalesOrderDetail AS Details
     INNER JOIN
       Production.Product AS PROD ON
          Details.ProductID = PROD.ProductID
     INNER JOIN 
       Sales.SalesOrderHeader AS OrdHead ON
         Details.SalesOrderID = OrdHead.SalesOrderID
GROUP BY 
         PROD.Name,
         YEAR(OrdHead.OrderDate)
ORDER BY [Order Year]

Although it is easy to understand the above query, we are copying the data to a new table to avoid further complications. It will also allow you to concentrate more on SQL Server Pivot. To do this, we are using SELECT INTO Statement in SQL Server. The below screenshot shows our new table data. We use the pivot function to convert row data into Columns

SQL PIVOT 1

SQL PIVOT Approach 1

In this example, we will convert the calendar year information in rows to separate columns using pivot. Here, the Name column will remain same, and the Order quantity will separate based on the Year

-- SQL Server PIVOT Example
SELECT Name,[2011], [2012], [2013], [2014]
FROM (
      SELECT [Name]
           ,[Order Year]
           ,[Order Quantity]
      FROM [SQL Pivot Source]
     ) AS SOURCE
PIVOT 
   ( 
    SUM([Order Quantity]) FOR [Order Year] 
	 IN ([2011], [2012], [2013], [2014])
   ) AS [PIVOT TABLE] 
ORDER BY Name
SQL PIVOT 2

Below Select query will execute first. and this will extract name, Order Year and Order Quantity information from the Pivot table. Please refer to the Select Statement and SELECT INTO Statement in SQL Server.

SELECT [Name]
      ,[Order Year]
      ,[Order Quantity]
 FROM [SQL Pivot Source]
     ) AS SOURCE

The following statement will convert the rows into columns using Pivot SQL

PIVOT 
   ( 
    SUM([Order Quantity]) FOR [Order Year] 
	 IN ([2011], [2012], [2013], [2014])
   ) AS [PIVOT TABLE]

The below statement will display the output

SELECT Name,[2011], [2012], [2013], [2014]

If you want to apply the same functionalities on your original data, You have to use the following Pivot query

USE AdventureWorks2014
GO
SELECT Name,[2011], [2012], [2013], [2014]
 FROM 
   (
    SELECT PROD.Name,
           YEAR(OrdHead.OrderDate) as OrderYear,
           SUM(Details.OrderQty) AS OrderQuantity
    FROM Sales.SalesOrderDetail AS Details
     INNER JOIN Production.Product AS PROD 
       ON Details.ProductID = PROD.ProductID
     INNER JOIN Sales.SalesOrderHeader AS OrdHead 
       ON Details.SalesOrderID = OrdHead.SalesOrderID
     GROUP BY PROD.Name,YEAR(OrdHead.OrderDate)
    ) AS SOURCE
PIVOT ( SUM(OrderQuantity) FOR OrderYear 
        IN ([2011], [2012], [2013], [2014])
       ) AS pivotable 
ORDER BY Name

PIVOT Approach 2

If you are getting all your source information from one table. And if the data doesn’t include any Joins and Grouping, use the following query to pivot the source table. It is a short version of SQL Server Pivot Approach 1

-- SQL Server PIVOT Example
SELECT Name,[2011], [2012], [2013], [2014]
FROM [SQL Pivot Source]
PIVOT 
   ( 
    SUM([Order Quantity]) FOR [Order Year] 
      IN ([2011], [2012], [2013], [2014])
   ) AS [PIVOT TABLE] 
ORDER BY Name
SQL PIVOT 3

SQL PIVOT Alternative

This alternative pivot example is purely for the interview purpose. Normally, To test your coding skills in the interview, they might ask you a question: How to convert rows to columns without using the built-in Pivot

-- SQL Server PIVOT Alternative 
SELECT 
    Name
   ,SUM(CASE WHEN [Order Year] = 2011 THEN [Order Quantity] ELSE 0 END)AS [2011]
   ,SUM(CASE WHEN [Order Year] = 2012 THEN [Order Quantity] ELSE 0 END)AS [2012]
   ,SUM(CASE WHEN [Order Year] = 2013 THEN [Order Quantity] ELSE 0 END)AS [2013]
   ,SUM(CASE WHEN [Order Year] = 2014 THEN [Order Quantity] ELSE 0 END)AS [2014]  
FROM [SQL Pivot Source]    
GROUP BY Name
ORDER BY Name
SQL PIVOT 4

If you want to apply the same functionality on your original data, You have to use the following pivot query

-- SQL Server PIVOT Alternative
USE AdventureWorks2014
GO
SELECT 
   PROD.Name
  ,SUM(CASE WHEN YEAR(OrdHead.OrderDate) = 2011 THEN (Details.OrderQty) ELSE 0 END)AS [2011]
  ,SUM(CASE WHEN YEAR(OrdHead.OrderDate) = 2012 THEN (Details.OrderQty) ELSE 0 END)AS [2012]
  ,SUM(CASE WHEN YEAR(OrdHead.OrderDate) = 2013 THEN (Details.OrderQty) ELSE 0 END)AS [2013]
  ,SUM(CASE WHEN YEAR(OrdHead.OrderDate) = 2014 THEN (Details.OrderQty) ELSE 0 END)AS [2014]
FROM Sales.SalesOrderDetail AS Details
  INNER JOIN Production.Product AS PROD 
    ON Details.ProductID = PROD.ProductID
  INNER JOIN Sales.SalesOrderHeader AS OrdHead 
    ON Details.SalesOrderID = OrdHead.SalesOrderID
GROUP BY PROD.Name
ORDER BY Name

Comments are closed.