Skip to main content

Posts

Showing posts from April, 2017

UNPIVOT in SQL Server!

A few weeks ago I did a tutorial on how to pivot data in MS SQL Server.  In some cases, one may need to unpivot data in order to place it in a normalized database structure.  Many MS Excel experts can easily manipulate data so they can see it any kind of way.  Similar tools exist in SQL Server, but obviously the ways to achieve the same ends are different.  Here is a visualization of the data: ID Another ID Week1 Week2 Week3 Week4 Week5 Week6 100 A11 200 500 300 600 450 100 So, in this situation a client may send us sales data for 6 weeks that looks like this and it needs to be not only placed in the database to archive it and join it to other entities or dimensions (if it is a data warehouse).  To make sure the data will go in a normalized table or a fact table, the data will need to be unpivoted to look like this: ID Another ID ...