SlideShare a Scribd company logo
1 of 8
How to use UNPIVOT in SQL
The UNPIVOT function in SQL is used to
turn a dataset from columns to rows
From This…
A wide table of columns
Such as this….
To this…..
A table of Year, Month and days per month
Data Set Up
If you want to follow this example, you can run the following SQL to create the dataset used in this example
CREATE TABLE [dbo].[UNPIVOT_Example](
[year] [int] NULL,
[January] [int] NULL,
[February] [int] NULL,
[March] [int] NULL,
[April] [int] NULL,
[May] [int] NULL,
[June] [int] NULL,
[July] [int] NULL,
[August] [int] NULL,
[September] [int] NULL,
[October] [int] NULL,
[November] [int] NULL,
[December] [int] NULL
) ON [PRIMARY]
GO
INSERT [dbo].[UNPIVOT_Example] ([year], [January], [February], [March], [April], [May], [June], [July],
[August], [September], [October], [November], [December])
VALUES
(2018, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31),
(2019, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31),
(2020, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31),
(2021, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31),
(2022, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
GO
select Year, Month, days
from [dbo].[UNPIVOT_Example]
UNPIVOT
(days
for Month in
([January]
, [February]
, [March]
, [April]
, [May]
, [June]
, [July]
, [August]
, [September]
, [October]
, [November]
, [December])
)as dpm_unpivot
SQL UNPIVOT
Here is the SQL code and a breakdown of what
each part does
3. This is the final set of columns that we want to
output
2. This is the start of the UNPIVOT function,
We define the column ‘MONTH’ using the full
list of existing column names
‘days’ is the new name for the column of data
that we will generate from the UNPIVOT
1. Here we select each
of the columns of data
that we want to be
unpivoted
Use Cases
SQL UNPIVOT is very useful when the data you have to work with is already in a PIVOTED style
Quite often we come across that is provided to us in this way, usually as outputs from proprietary
systems
Converting these to tabular data sets makes working with the data much easier, and allows far
simpler work if you need to aggregate all the data by year
Hope you found this useful
For more Tips, Tricks and
Timesavers, visit our website
Tips and Timesavers | Select Distinct Limited
Credit: Simon.harrison@select distinct limited

More Related Content

More from Select Distinct Limited

Calculated Columns and Measures in Power BI.pptx
Calculated Columns and Measures in Power BI.pptxCalculated Columns and Measures in Power BI.pptx
Calculated Columns and Measures in Power BI.pptxSelect Distinct Limited
 
When to transform data for Power BI.pptx
When to transform data for Power BI.pptxWhen to transform data for Power BI.pptx
When to transform data for Power BI.pptxSelect Distinct Limited
 
Direction of travel on a map in Power BI.pptx
Direction of travel on a map in Power BI.pptxDirection of travel on a map in Power BI.pptx
Direction of travel on a map in Power BI.pptxSelect Distinct Limited
 
Power BI Connect to Google BigQuery.pptx
Power BI Connect to Google BigQuery.pptxPower BI Connect to Google BigQuery.pptx
Power BI Connect to Google BigQuery.pptxSelect Distinct Limited
 
How to use a slicer to toggle measures in Power BI
How to use a slicer to toggle measures in Power BIHow to use a slicer to toggle measures in Power BI
How to use a slicer to toggle measures in Power BISelect Distinct Limited
 
Power BI Tips Rolling Averages and Rolling Sums.pptx
Power BI Tips Rolling Averages and Rolling Sums.pptxPower BI Tips Rolling Averages and Rolling Sums.pptx
Power BI Tips Rolling Averages and Rolling Sums.pptxSelect Distinct Limited
 
Power BI Templates and how to use them.pptx
Power BI Templates and how to use them.pptxPower BI Templates and how to use them.pptx
Power BI Templates and how to use them.pptxSelect Distinct Limited
 

More from Select Distinct Limited (20)

Calculated Columns and Measures in Power BI.pptx
Calculated Columns and Measures in Power BI.pptxCalculated Columns and Measures in Power BI.pptx
Calculated Columns and Measures in Power BI.pptx
 
Divide by Zero Errors
Divide by Zero ErrorsDivide by Zero Errors
Divide by Zero Errors
 
When to transform data for Power BI.pptx
When to transform data for Power BI.pptxWhen to transform data for Power BI.pptx
When to transform data for Power BI.pptx
 
Power BI KPIs
Power BI KPIsPower BI KPIs
Power BI KPIs
 
Direction of travel on a map in Power BI.pptx
Direction of travel on a map in Power BI.pptxDirection of travel on a map in Power BI.pptx
Direction of travel on a map in Power BI.pptx
 
UNION in DAX
UNION in DAXUNION in DAX
UNION in DAX
 
APPEND data in Power Query
APPEND data in Power QueryAPPEND data in Power Query
APPEND data in Power Query
 
Power BI Connect to Google BigQuery.pptx
Power BI Connect to Google BigQuery.pptxPower BI Connect to Google BigQuery.pptx
Power BI Connect to Google BigQuery.pptx
 
Group by ROLLUP in SQL Server
Group by ROLLUP in SQL ServerGroup by ROLLUP in SQL Server
Group by ROLLUP in SQL Server
 
Advanced Top N Measures in Power BI
Advanced Top N Measures in Power BIAdvanced Top N Measures in Power BI
Advanced Top N Measures in Power BI
 
Custom Formats in Power BI
Custom Formats in Power BICustom Formats in Power BI
Custom Formats in Power BI
 
Power BI Tips Top N Measures.pptx
Power BI Tips Top N Measures.pptxPower BI Tips Top N Measures.pptx
Power BI Tips Top N Measures.pptx
 
SQL Tips UNPIVOT Function.pptx
SQL Tips UNPIVOT Function.pptxSQL Tips UNPIVOT Function.pptx
SQL Tips UNPIVOT Function.pptx
 
Power BI Drill Through
Power BI Drill ThroughPower BI Drill Through
Power BI Drill Through
 
How to PIVOT in SQL
How to PIVOT in SQLHow to PIVOT in SQL
How to PIVOT in SQL
 
How to use a slicer to toggle measures in Power BI
How to use a slicer to toggle measures in Power BIHow to use a slicer to toggle measures in Power BI
How to use a slicer to toggle measures in Power BI
 
Power BI Tips Rolling Averages and Rolling Sums.pptx
Power BI Tips Rolling Averages and Rolling Sums.pptxPower BI Tips Rolling Averages and Rolling Sums.pptx
Power BI Tips Rolling Averages and Rolling Sums.pptx
 
Power BI Templates and how to use them.pptx
Power BI Templates and how to use them.pptxPower BI Templates and how to use them.pptx
Power BI Templates and how to use them.pptx
 
How to use Format painter in Excel
How to use Format painter in ExcelHow to use Format painter in Excel
How to use Format painter in Excel
 
SQL Select Distinct Statement
SQL Select Distinct StatementSQL Select Distinct Statement
SQL Select Distinct Statement
 

SQL Tips UNPIVOT Function.pptx

  • 1. How to use UNPIVOT in SQL
  • 2. The UNPIVOT function in SQL is used to turn a dataset from columns to rows
  • 3. From This… A wide table of columns Such as this…. To this….. A table of Year, Month and days per month
  • 4. Data Set Up If you want to follow this example, you can run the following SQL to create the dataset used in this example CREATE TABLE [dbo].[UNPIVOT_Example]( [year] [int] NULL, [January] [int] NULL, [February] [int] NULL, [March] [int] NULL, [April] [int] NULL, [May] [int] NULL, [June] [int] NULL, [July] [int] NULL, [August] [int] NULL, [September] [int] NULL, [October] [int] NULL, [November] [int] NULL, [December] [int] NULL ) ON [PRIMARY] GO INSERT [dbo].[UNPIVOT_Example] ([year], [January], [February], [March], [April], [May], [June], [July], [August], [September], [October], [November], [December]) VALUES (2018, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31), (2019, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31), (2020, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31), (2021, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31), (2022, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) GO
  • 5. select Year, Month, days from [dbo].[UNPIVOT_Example] UNPIVOT (days for Month in ([January] , [February] , [March] , [April] , [May] , [June] , [July] , [August] , [September] , [October] , [November] , [December]) )as dpm_unpivot SQL UNPIVOT Here is the SQL code and a breakdown of what each part does 3. This is the final set of columns that we want to output 2. This is the start of the UNPIVOT function, We define the column ‘MONTH’ using the full list of existing column names ‘days’ is the new name for the column of data that we will generate from the UNPIVOT 1. Here we select each of the columns of data that we want to be unpivoted
  • 6. Use Cases SQL UNPIVOT is very useful when the data you have to work with is already in a PIVOTED style Quite often we come across that is provided to us in this way, usually as outputs from proprietary systems Converting these to tabular data sets makes working with the data much easier, and allows far simpler work if you need to aggregate all the data by year
  • 7. Hope you found this useful
  • 8. For more Tips, Tricks and Timesavers, visit our website Tips and Timesavers | Select Distinct Limited Credit: Simon.harrison@select distinct limited