SQL Server UNPIVOT

How to use a SQL Server unpivot query to transform columns to rows? It’s possible with this simple T-SQL un-pivot function example. The T-SQL Unpivot function can do it in one single query. It is the exact opposite of the PIVOT query.

In this example, only the six first months of the year are used as columns, namely January to June. But you can extend the query to all 12 periods of the year by adding the missing ones. 

Script to transform columns to rows with the SQL Server UNPIVOT function

For example, the SQL Server Sales table contains one type column and the month’s columns. They contain respectively the type and the amount of sales for the month. To start, just copy and paste the SQL query for the table creation and data insertion. Add the missing months if necessary. This way you will be able to transform the 12 months of the year from columns to rows in one single SQL Server query.

Create the sample table to transform columns to rows using the UNPIVOT function

First of all, create the sample table using this script, simply copy and paste the example in your SQL Server Management Studio window.

-- First of all DROP the Sales by month table if it already exists in the db
IF EXISTS( 
	SELECT 1 FROM sys.objects
     WHERE object_id = object_id(N'[dbo].[SALES_BY_MONTH]')
		AND type in (N'U') )
-- BEGIN DROP TABLE [dbo].[SALES_BY_MONTH]
END;

-- Create the SALES table
CREATE table [dbo].[SALES_BY_MONTH] (
    [SalesType] NVARCHAR(20),
    [January] NUMERIC(5),
    [February] NUMERIC(5),
    [March] NUMERIC(5),
    [April] NUMERIC(5),
    [May] NUMERIC(5),
    [June] NUMERIC(5)
);

-- Insert sample sales data for each month: i.e. from January to June
-- The month columns will be pivoted from columns to lines
INSERT INTO dbo.SALES_BY_MONTH ( [SalesType], [January], [February], [March], [April], [May], [June] )
VALUES ( N'Sales', 1000, 2000, 3000, 4000, 5000, 6000);

INSERT INTO dbo.SALES_BY_MONTH ( [SalesType], [January], [February], [March], [April], [May], [June] )
VALUES ( N'Discounts', 100, 200, 300, 400, 500, 600);

INSERT INTO dbo.SALES_BY_MONTH ( [SalesType], [January], [February], [March], [April], [May], [June] )
VALUES ( N'Offers', 10, 20, 30, 40, 50, 60);

-- Check the lines inserted in the sales table
SELECT * 
FROM dbo.SALES_BY_MONTH;
Sample Sales data to transform from columns to rows with the SQL Server UNPIVOT function
Sample Sales data to transform from columns to rows with the SQL Server UNPIVOT function

Write the SQL Server UNPIVOT query to transform columns into rows

Finally, the SQL Server UNPIVOT function is compound of 3 different steps  to transpose the columns in rows.

  1. The selection of the 3 result columns, i.e. Type, Month and Amount.
  2. Then the SQL sub-query with the original selection of data.
  3. The UNPIVOT operation itself with the 6 months named explicitly.
-- UNPIVOT Columns to Rows with SQL Query
SELECT [TYPE], Month, Amount
FROM (	
	SELECT [TYPE], [January], [February], [March], [April], [May], [June]
	FROM dbo.SALES_BY_MONTH 	) sbm
UNPIVOT
   (Amount FOR Month IN 
      ([January], [February], [March], [April], [May], [June])
)AS SalesUnPivot;
T-SQL query with SQL Server UNPIVOT function and the result
T-SQL query with SQL Server UNPIVOT function and the result

In addition, if you are still struggling with the UNPIVOT function syntax ? Just copy paste the example and repeat the three steps by adapting to your real case. Start by renaming the columns to understand how it works.

In conclusion, to read mode details, the official Microsoft documentation for the unpivot function is available.

The UNPIVOT is the opposite of the PIVOT function, here is another short tutorial to use the SQL Server PIVOT function.

https://expert-only.com/en/t-sql/sql-server-pivot-example/

2 thoughts on “SQL Server UNPIVOT”

Leave a Comment

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

Scroll to Top