Unlike PIVOT which de-normalizes the data, UNPIVOT is more helpful in getting your data back to a more normalized form.
UNPIVOT performs the opposite operation to PIVOT by rotating columns of a table-valued expression into column values.
For a single column UNPIVOT examples and for more on PIVOT and UNPIVOT you can read from Microsoft Books Online - Using PIVOT and UNPIVOT
In this article I’ll try to show you how to UNPIVOT multiple columns in a single query. There is simple trick to do that, if you are familiar with the UNPIVOT operator you will find it easy to understand.
First lets create some sample data :
-- Create sample table -- Load Sample data -- Test sample data |
Here is how our sample table looks like :
Id | Product | Supplier1 | Supplier2 | Supplier3 | City1 | City2 | City3 |
1 | Car | TataMotors | Bajaj | Mahindra | Jamshedpur | Pune | Mumbai |
2 | Bike | Bajaj | Hero Honda | Suzuki | Pune | New Delhi | Chandigarh |
3 | Cycle | Hercules | Hero | Atlas | Mumbai | Bangalore | Pune |
And as you can see here we have 2 columns which needs to be UNPIVOT, first is Supplier and second is there respective Cities. If it was single column to be UNPIVOTed it could have been a straight forward task. Thing is you can write a query with 2 UNPIVOT operators, but question is how to JOIN two UNPIVOT sub queries correctly so we will get the each supplier with its respective city next to it in second column?
As I said earlier there is simple trick to do that. See the following query and its WHERE condition :
SELECT Id, |
And here is the Output of the query :
Id | Product | SuppId | SupplierName | CityName |
1 | Car | 1 | Tata Motors | Jamshedpur |
1 | Car | 2 | Bajaj | Pune |
1 | Car | 3 | Mahindra | Mumbai |
2 | Bike | 1 | Bajaj | Pune |
2 | Bike | 2 | Hero Honda | New Delhi |
2 | Bike | 3 | Suzuki | Chandigarh |
3 | Cycle | 1 | Hercules | Mumbai |
3 | Cycle | 2 | Hero | Bangalore |
3 | Cycle | 3 | Atlas | Pune |
So the trick was this where condition -
WHERE RIGHT(Suppliers,1) = RIGHT(Cities,1)
There are other ways also to UNPIVOT the multiple columns, may be some other time I’ll post them too.
Source of inspiration for this post is following thread on MSDN forums - http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/54626179-ee9f-4db7-a31e-c02104bdd6beAt
At the end of it, OP also come up with his own solution he got from SQLTeam’s forums.
Mangal Pardeshi
That works like a charm! You might want to explain clearly that it is the last letter of the columns that makes the magic here:
ReplyDeleteSupplier1 corresponds with City1
Supplier2 corresponds with City2
etc.
Thanks anyway!
Yes, needed that bit explained to understand what was going on...thank you!
DeleteOh Mangal, thank you so much for your post. It saved my day. It took me a while to figureout with my data on "how to do this exactly to return right data"
ReplyDeleteAnd thanks to Stian Sveen for that clue.
Gr8 job guys!
Please post the details of query[please explain all line by line]
ReplyDeleteNice artcie, Good work.
ReplyDeleteNice Artcile, Good work.
ReplyDeletethis did not work
ReplyDeleteI tried this and nothing returned back. This does not work the way it is shown.
ReplyDeleteNice and easy!
ReplyDeleteNice and easy. Good job!
ReplyDeleteFor those who didnt got any records, the trick is Column Names must end with 1,2,3....
ReplyDeleteFor those who didnt got any records, the trick is Column Names must end with 1,2,3....
ReplyDeleteThis is tricky, and heavily dependent on column names. i am looking for solution to similar problem, but independent on column names.
ReplyDeleteThis is tricky and dependent on column names. I am looking for similar solution that is independent on column name
ReplyDeletethanks, this examples help me a lot
ReplyDeletethanks, this example help me a lot
ReplyDeleteUsed this to get tabular data for a select statement that I used, this was simple and straight to the point.
ReplyDeleteYo very slick. Dig it!!!
ReplyDeleteI worked with this guys.. He is genus..
ReplyDeleteThanks.... It helped me a lot
ReplyDeletebut when you have more than 10 cities and suppliers it doesn't work. how can u fix it when is more than 10? in 11 it breaks
ReplyDeletein (WHERE RIGHT(Suppliers,1) = RIGHT(Cities,1))
ReplyDeletefrom where you have Suppliers and Cities??
Thanks for the great solution, Mangal!
ReplyDeleteThank you. This is exactly what I needed.
ReplyDeleteThank you. this was really useful.
ReplyDeleteThanks for posting this, very helpful.
ReplyDeleteIf the number of repeating columns go over 9, then the column names would need to be Supplier01, Supplier02, ...
and the WHERE clause would need to look at the 2 most right characters
WHERE RIGHT(Suppliers,1) = RIGHT(Cities,1)