SlideShare a Scribd company logo
1 of 8
SQL Tips
and Timesavers
How to use
The UNPIVOT function in
SQL
Introduction
Syntax
Use Cases
The UNPIVOT function in SQL is used to
transform a dataset from columns to
rows
(Into a normalized table)
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
UNPIVOT is a powerful and useful operator that can help you transform
your data in SQL Server.
You can use it to normalize your tables, or to reverse the effect of a PIVOT
operation.
You can also combine it with other operators and functions to perform
more complex transformations.
For more Tips, Tricks and
Timesavers, visit our website
Business Analytics Blog – Select Distinct
Credit: simon.harrison@selectdistinct.co.uk

More Related Content

Similar to SQL Tips UNPIVOT Function.pptx

Data Analysis – Technical learnings
Data Analysis – Technical learningsData Analysis – Technical learnings
Data Analysis – Technical learningsInvenkLearn
 
Excel Useful Tips
Excel Useful TipsExcel Useful Tips
Excel Useful TipsParul_100in
 
PHP Detroit -- MySQL 8 A New Beginning (updated presentation)
PHP Detroit -- MySQL 8 A New Beginning (updated presentation)PHP Detroit -- MySQL 8 A New Beginning (updated presentation)
PHP Detroit -- MySQL 8 A New Beginning (updated presentation)Dave Stokes
 
Cincinnati Tableau User Group Event #1
Cincinnati Tableau User Group Event #1Cincinnati Tableau User Group Event #1
Cincinnati Tableau User Group Event #1Russell Spangler
 
Oracle_Analytical_function.pdf
Oracle_Analytical_function.pdfOracle_Analytical_function.pdf
Oracle_Analytical_function.pdfKalyankumarVenkat1
 
Excel vs Tableau the comparison you should know
Excel vs Tableau  the comparison you should knowExcel vs Tableau  the comparison you should know
Excel vs Tableau the comparison you should knowStat Analytica
 
Business Intelligence Portfolio
Business Intelligence PortfolioBusiness Intelligence Portfolio
Business Intelligence PortfolioChris Seebacher
 
A pivot table is a table which is used to store the summary of a cer.pdf
A pivot table is a table which is used to store the summary of a cer.pdfA pivot table is a table which is used to store the summary of a cer.pdf
A pivot table is a table which is used to store the summary of a cer.pdfaptcomputerzone
 
Join 2017_Deep Dive_Table Calculations 201
Join 2017_Deep Dive_Table Calculations 201Join 2017_Deep Dive_Table Calculations 201
Join 2017_Deep Dive_Table Calculations 201Looker
 
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdfAbstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdfkarymadelaneyrenne19
 
The International Journal of Engineering and Science (The IJES)
The International Journal of Engineering and Science (The IJES)The International Journal of Engineering and Science (The IJES)
The International Journal of Engineering and Science (The IJES)theijes
 
1a s4 i creating runcharts final
1a s4 i creating runcharts final1a s4 i creating runcharts final
1a s4 i creating runcharts finalABCiABUHB
 
10 SQL Server Metrics to Monitor
10 SQL Server Metrics to Monitor10 SQL Server Metrics to Monitor
10 SQL Server Metrics to MonitorSQLDBApros
 
Intro to SQL for Beginners
Intro to SQL for BeginnersIntro to SQL for Beginners
Intro to SQL for BeginnersProduct School
 

Similar to SQL Tips UNPIVOT Function.pptx (20)

Data Analysis – Technical learnings
Data Analysis – Technical learningsData Analysis – Technical learnings
Data Analysis – Technical learnings
 
Office
OfficeOffice
Office
 
Excel Useful Tips
Excel Useful TipsExcel Useful Tips
Excel Useful Tips
 
Group by ROLLUP in SQL Server
Group by ROLLUP in SQL ServerGroup by ROLLUP in SQL Server
Group by ROLLUP in SQL Server
 
It ready dw_day4_rev00
It ready dw_day4_rev00It ready dw_day4_rev00
It ready dw_day4_rev00
 
PHP Detroit -- MySQL 8 A New Beginning (updated presentation)
PHP Detroit -- MySQL 8 A New Beginning (updated presentation)PHP Detroit -- MySQL 8 A New Beginning (updated presentation)
PHP Detroit -- MySQL 8 A New Beginning (updated presentation)
 
Cincinnati Tableau User Group Event #1
Cincinnati Tableau User Group Event #1Cincinnati Tableau User Group Event #1
Cincinnati Tableau User Group Event #1
 
Oracle_Analytical_function.pdf
Oracle_Analytical_function.pdfOracle_Analytical_function.pdf
Oracle_Analytical_function.pdf
 
Excel vs Tableau the comparison you should know
Excel vs Tableau  the comparison you should knowExcel vs Tableau  the comparison you should know
Excel vs Tableau the comparison you should know
 
Business Intelligence Portfolio
Business Intelligence PortfolioBusiness Intelligence Portfolio
Business Intelligence Portfolio
 
REPORT ON (1)
REPORT ON (1)REPORT ON (1)
REPORT ON (1)
 
A pivot table is a table which is used to store the summary of a cer.pdf
A pivot table is a table which is used to store the summary of a cer.pdfA pivot table is a table which is used to store the summary of a cer.pdf
A pivot table is a table which is used to store the summary of a cer.pdf
 
Join 2017_Deep Dive_Table Calculations 201
Join 2017_Deep Dive_Table Calculations 201Join 2017_Deep Dive_Table Calculations 201
Join 2017_Deep Dive_Table Calculations 201
 
SQL for interview
SQL for interviewSQL for interview
SQL for interview
 
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdfAbstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
 
The International Journal of Engineering and Science (The IJES)
The International Journal of Engineering and Science (The IJES)The International Journal of Engineering and Science (The IJES)
The International Journal of Engineering and Science (The IJES)
 
1a s4 i creating runcharts final
1a s4 i creating runcharts final1a s4 i creating runcharts final
1a s4 i creating runcharts final
 
10 SQL Server Metrics to Monitor
10 SQL Server Metrics to Monitor10 SQL Server Metrics to Monitor
10 SQL Server Metrics to Monitor
 
Intro to SQL for Beginners
Intro to SQL for BeginnersIntro to SQL for Beginners
Intro to SQL for Beginners
 
Uses of excel
Uses of excelUses of excel
Uses of excel
 

More from Select Distinct Limited

Year on Year Comparison in Power BI.pptx
Year on Year Comparison in Power BI.pptxYear on Year Comparison in Power BI.pptx
Year on Year Comparison in Power BI.pptxSelect Distinct Limited
 
Sync Slicers in Power BI a step by step guide
Sync Slicers in Power BI a step by step guideSync Slicers in Power BI a step by step guide
Sync Slicers in Power BI a step by step guideSelect Distinct Limited
 
Using Google Search Console Data in Power BI.pptx
Using Google Search Console Data in Power BI.pptxUsing Google Search Console Data in Power BI.pptx
Using Google Search Console Data in Power BI.pptxSelect Distinct Limited
 
Data Lake v Data Warehouse. What is the difference?
Data Lake v Data Warehouse. What is the difference?Data Lake v Data Warehouse. What is the difference?
Data Lake v Data Warehouse. What is the difference?Select Distinct Limited
 
How to Create Drop Down Lists in Excel, step by step
How to Create Drop Down Lists in Excel, step by stepHow to Create Drop Down Lists in Excel, step by step
How to Create Drop Down Lists in Excel, step by stepSelect Distinct Limited
 
Top 5 Power BI tips 2023 most popular blog posts
Top 5 Power BI tips 2023 most popular blog postsTop 5 Power BI tips 2023 most popular blog posts
Top 5 Power BI tips 2023 most popular blog postsSelect 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
 

More from Select Distinct Limited (20)

Year on Year Comparison in Power BI.pptx
Year on Year Comparison in Power BI.pptxYear on Year Comparison in Power BI.pptx
Year on Year Comparison in Power BI.pptx
 
Sync Slicers in Power BI a step by step guide
Sync Slicers in Power BI a step by step guideSync Slicers in Power BI a step by step guide
Sync Slicers in Power BI a step by step guide
 
Using Google Search Console Data in Power BI.pptx
Using Google Search Console Data in Power BI.pptxUsing Google Search Console Data in Power BI.pptx
Using Google Search Console Data in Power BI.pptx
 
Data Lake v Data Warehouse. What is the difference?
Data Lake v Data Warehouse. What is the difference?Data Lake v Data Warehouse. What is the difference?
Data Lake v Data Warehouse. What is the difference?
 
How to Create Drop Down Lists in Excel, step by step
How to Create Drop Down Lists in Excel, step by stepHow to Create Drop Down Lists in Excel, step by step
How to Create Drop Down Lists in Excel, step by step
 
Top 5 Power BI tips 2023 most popular blog posts
Top 5 Power BI tips 2023 most popular blog postsTop 5 Power BI tips 2023 most popular blog posts
Top 5 Power BI tips 2023 most popular blog posts
 
CTEs in SQL.pptx
CTEs in SQL.pptxCTEs in SQL.pptx
CTEs in SQL.pptx
 
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
 
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
 
Power BI Drill Through
Power BI Drill ThroughPower BI Drill Through
Power BI Drill Through
 
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
 

Recently uploaded

社内勉強会資料_Object Recognition as Next Token Prediction
社内勉強会資料_Object Recognition as Next Token Prediction社内勉強会資料_Object Recognition as Next Token Prediction
社内勉強会資料_Object Recognition as Next Token PredictionNABLAS株式会社
 
如何办理(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单本科硕士学位证留信学历认证如何办理(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单本科硕士学位证留信学历认证acoha1
 
Audience Researchndfhcvnfgvgbhujhgfv.pptx
Audience Researchndfhcvnfgvgbhujhgfv.pptxAudience Researchndfhcvnfgvgbhujhgfv.pptx
Audience Researchndfhcvnfgvgbhujhgfv.pptxStephen266013
 
obat aborsi Tarakan wa 081336238223 jual obat aborsi cytotec asli di Tarakan9...
obat aborsi Tarakan wa 081336238223 jual obat aborsi cytotec asli di Tarakan9...obat aborsi Tarakan wa 081336238223 jual obat aborsi cytotec asli di Tarakan9...
obat aborsi Tarakan wa 081336238223 jual obat aborsi cytotec asli di Tarakan9...yulianti213969
 
Bios of leading Astrologers & Researchers
Bios of leading Astrologers & ResearchersBios of leading Astrologers & Researchers
Bios of leading Astrologers & Researchersdarmandersingh4580
 
obat aborsi Bontang wa 082135199655 jual obat aborsi cytotec asli di Bontang
obat aborsi Bontang wa 082135199655 jual obat aborsi cytotec asli di  Bontangobat aborsi Bontang wa 082135199655 jual obat aborsi cytotec asli di  Bontang
obat aborsi Bontang wa 082135199655 jual obat aborsi cytotec asli di Bontangsiskavia95
 
Genuine love spell caster )! ,+27834335081) Ex lover back permanently in At...
Genuine love spell caster )! ,+27834335081)   Ex lover back permanently in At...Genuine love spell caster )! ,+27834335081)   Ex lover back permanently in At...
Genuine love spell caster )! ,+27834335081) Ex lover back permanently in At...BabaJohn3
 
Northern New England Tableau User Group (TUG) May 2024
Northern New England Tableau User Group (TUG) May 2024Northern New England Tableau User Group (TUG) May 2024
Northern New England Tableau User Group (TUG) May 2024patrickdtherriault
 
Seven tools of quality control.slideshare
Seven tools of quality control.slideshareSeven tools of quality control.slideshare
Seven tools of quality control.slideshareraiaryan448
 
Identify Rules that Predict Patient’s Heart Disease - An Application of Decis...
Identify Rules that Predict Patient’s Heart Disease - An Application of Decis...Identify Rules that Predict Patient’s Heart Disease - An Application of Decis...
Identify Rules that Predict Patient’s Heart Disease - An Application of Decis...ThinkInnovation
 
Credit Card Fraud Detection: Safeguarding Transactions in the Digital Age
Credit Card Fraud Detection: Safeguarding Transactions in the Digital AgeCredit Card Fraud Detection: Safeguarding Transactions in the Digital Age
Credit Card Fraud Detection: Safeguarding Transactions in the Digital AgeBoston Institute of Analytics
 
Formulas dax para power bI de microsoft.pdf
Formulas dax para power bI de microsoft.pdfFormulas dax para power bI de microsoft.pdf
Formulas dax para power bI de microsoft.pdfRobertoOcampo24
 
如何办理(Dalhousie毕业证书)达尔豪斯大学毕业证成绩单留信学历认证
如何办理(Dalhousie毕业证书)达尔豪斯大学毕业证成绩单留信学历认证如何办理(Dalhousie毕业证书)达尔豪斯大学毕业证成绩单留信学历认证
如何办理(Dalhousie毕业证书)达尔豪斯大学毕业证成绩单留信学历认证zifhagzkk
 
What is Insertion Sort. Its basic information
What is Insertion Sort. Its basic informationWhat is Insertion Sort. Its basic information
What is Insertion Sort. Its basic informationmuqadasqasim10
 
如何办理(WashU毕业证书)圣路易斯华盛顿大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(WashU毕业证书)圣路易斯华盛顿大学毕业证成绩单本科硕士学位证留信学历认证如何办理(WashU毕业证书)圣路易斯华盛顿大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(WashU毕业证书)圣路易斯华盛顿大学毕业证成绩单本科硕士学位证留信学历认证acoha1
 
NOAM AAUG Adobe Summit 2024: Summit Slam Dunks
NOAM AAUG Adobe Summit 2024: Summit Slam DunksNOAM AAUG Adobe Summit 2024: Summit Slam Dunks
NOAM AAUG Adobe Summit 2024: Summit Slam Dunksgmuir1066
 
Identify Customer Segments to Create Customer Offers for Each Segment - Appli...
Identify Customer Segments to Create Customer Offers for Each Segment - Appli...Identify Customer Segments to Create Customer Offers for Each Segment - Appli...
Identify Customer Segments to Create Customer Offers for Each Segment - Appli...ThinkInnovation
 
一比一原版(ucla文凭证书)加州大学洛杉矶分校毕业证学历认证官方成绩单
一比一原版(ucla文凭证书)加州大学洛杉矶分校毕业证学历认证官方成绩单一比一原版(ucla文凭证书)加州大学洛杉矶分校毕业证学历认证官方成绩单
一比一原版(ucla文凭证书)加州大学洛杉矶分校毕业证学历认证官方成绩单aqpto5bt
 
MATERI MANAJEMEN OF PENYAKIT TETANUS.ppt
MATERI  MANAJEMEN OF PENYAKIT TETANUS.pptMATERI  MANAJEMEN OF PENYAKIT TETANUS.ppt
MATERI MANAJEMEN OF PENYAKIT TETANUS.pptRachmaGhifari
 

Recently uploaded (20)

社内勉強会資料_Object Recognition as Next Token Prediction
社内勉強会資料_Object Recognition as Next Token Prediction社内勉強会資料_Object Recognition as Next Token Prediction
社内勉強会資料_Object Recognition as Next Token Prediction
 
如何办理(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单本科硕士学位证留信学历认证如何办理(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单本科硕士学位证留信学历认证
 
Audience Researchndfhcvnfgvgbhujhgfv.pptx
Audience Researchndfhcvnfgvgbhujhgfv.pptxAudience Researchndfhcvnfgvgbhujhgfv.pptx
Audience Researchndfhcvnfgvgbhujhgfv.pptx
 
obat aborsi Tarakan wa 081336238223 jual obat aborsi cytotec asli di Tarakan9...
obat aborsi Tarakan wa 081336238223 jual obat aborsi cytotec asli di Tarakan9...obat aborsi Tarakan wa 081336238223 jual obat aborsi cytotec asli di Tarakan9...
obat aborsi Tarakan wa 081336238223 jual obat aborsi cytotec asli di Tarakan9...
 
Bios of leading Astrologers & Researchers
Bios of leading Astrologers & ResearchersBios of leading Astrologers & Researchers
Bios of leading Astrologers & Researchers
 
obat aborsi Bontang wa 082135199655 jual obat aborsi cytotec asli di Bontang
obat aborsi Bontang wa 082135199655 jual obat aborsi cytotec asli di  Bontangobat aborsi Bontang wa 082135199655 jual obat aborsi cytotec asli di  Bontang
obat aborsi Bontang wa 082135199655 jual obat aborsi cytotec asli di Bontang
 
Genuine love spell caster )! ,+27834335081) Ex lover back permanently in At...
Genuine love spell caster )! ,+27834335081)   Ex lover back permanently in At...Genuine love spell caster )! ,+27834335081)   Ex lover back permanently in At...
Genuine love spell caster )! ,+27834335081) Ex lover back permanently in At...
 
Northern New England Tableau User Group (TUG) May 2024
Northern New England Tableau User Group (TUG) May 2024Northern New England Tableau User Group (TUG) May 2024
Northern New England Tableau User Group (TUG) May 2024
 
Seven tools of quality control.slideshare
Seven tools of quality control.slideshareSeven tools of quality control.slideshare
Seven tools of quality control.slideshare
 
Identify Rules that Predict Patient’s Heart Disease - An Application of Decis...
Identify Rules that Predict Patient’s Heart Disease - An Application of Decis...Identify Rules that Predict Patient’s Heart Disease - An Application of Decis...
Identify Rules that Predict Patient’s Heart Disease - An Application of Decis...
 
Credit Card Fraud Detection: Safeguarding Transactions in the Digital Age
Credit Card Fraud Detection: Safeguarding Transactions in the Digital AgeCredit Card Fraud Detection: Safeguarding Transactions in the Digital Age
Credit Card Fraud Detection: Safeguarding Transactions in the Digital Age
 
Abortion pills in Riyadh Saudi Arabia (+966572737505 buy cytotec
Abortion pills in Riyadh Saudi Arabia (+966572737505 buy cytotecAbortion pills in Riyadh Saudi Arabia (+966572737505 buy cytotec
Abortion pills in Riyadh Saudi Arabia (+966572737505 buy cytotec
 
Formulas dax para power bI de microsoft.pdf
Formulas dax para power bI de microsoft.pdfFormulas dax para power bI de microsoft.pdf
Formulas dax para power bI de microsoft.pdf
 
如何办理(Dalhousie毕业证书)达尔豪斯大学毕业证成绩单留信学历认证
如何办理(Dalhousie毕业证书)达尔豪斯大学毕业证成绩单留信学历认证如何办理(Dalhousie毕业证书)达尔豪斯大学毕业证成绩单留信学历认证
如何办理(Dalhousie毕业证书)达尔豪斯大学毕业证成绩单留信学历认证
 
What is Insertion Sort. Its basic information
What is Insertion Sort. Its basic informationWhat is Insertion Sort. Its basic information
What is Insertion Sort. Its basic information
 
如何办理(WashU毕业证书)圣路易斯华盛顿大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(WashU毕业证书)圣路易斯华盛顿大学毕业证成绩单本科硕士学位证留信学历认证如何办理(WashU毕业证书)圣路易斯华盛顿大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(WashU毕业证书)圣路易斯华盛顿大学毕业证成绩单本科硕士学位证留信学历认证
 
NOAM AAUG Adobe Summit 2024: Summit Slam Dunks
NOAM AAUG Adobe Summit 2024: Summit Slam DunksNOAM AAUG Adobe Summit 2024: Summit Slam Dunks
NOAM AAUG Adobe Summit 2024: Summit Slam Dunks
 
Identify Customer Segments to Create Customer Offers for Each Segment - Appli...
Identify Customer Segments to Create Customer Offers for Each Segment - Appli...Identify Customer Segments to Create Customer Offers for Each Segment - Appli...
Identify Customer Segments to Create Customer Offers for Each Segment - Appli...
 
一比一原版(ucla文凭证书)加州大学洛杉矶分校毕业证学历认证官方成绩单
一比一原版(ucla文凭证书)加州大学洛杉矶分校毕业证学历认证官方成绩单一比一原版(ucla文凭证书)加州大学洛杉矶分校毕业证学历认证官方成绩单
一比一原版(ucla文凭证书)加州大学洛杉矶分校毕业证学历认证官方成绩单
 
MATERI MANAJEMEN OF PENYAKIT TETANUS.ppt
MATERI  MANAJEMEN OF PENYAKIT TETANUS.pptMATERI  MANAJEMEN OF PENYAKIT TETANUS.ppt
MATERI MANAJEMEN OF PENYAKIT TETANUS.ppt
 

SQL Tips UNPIVOT Function.pptx

  • 1. SQL Tips and Timesavers How to use The UNPIVOT function in SQL Introduction Syntax Use Cases
  • 2. The UNPIVOT function in SQL is used to transform a dataset from columns to rows (Into a normalized table)
  • 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. UNPIVOT is a powerful and useful operator that can help you transform your data in SQL Server. You can use it to normalize your tables, or to reverse the effect of a PIVOT operation. You can also combine it with other operators and functions to perform more complex transformations.
  • 8. For more Tips, Tricks and Timesavers, visit our website Business Analytics Blog – Select Distinct Credit: simon.harrison@selectdistinct.co.uk