Using this T-SQL example, you can easily transform columns into rows with just one step thanks to the SQL Server UNPIVOT function.
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.
Table of Contents
1. Create data to transform into rows using UNPIVOT operator
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 inside SSMS.
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) );
2. Insert data in the source table to transform
Next, insert the data in the table to be transposed. Each SQL data insertion adds 12 new sales amounts. One column per month, with the associated sales type.
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.
-- 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;
3. 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.
- The selection of the 3 result columns, i.e. Type, Month and Amount.
- Then the SQL sub-query with the original selection of data.
- 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;
About the PIVOT and UNPIVOT SQL Server Operators
In addition, if you are still struggling with this SQL Server UNPIVOT operator and its 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.
Finally a solid unpivot example that includes headers, thank you!
Thanks for the comment. I hope it helps, the syntax is a bit uncommon.