May 7, 2008 at 4:35 am
Is it possible to convert a number of columns to rows? I have a table which lists the months of the year as columns i.e. Jan, Feb, Mar etc are seperate columns within the table. Each column represents the value of sales for that month. What I want to do is have the months as rows and somehow have the value of sales associated.
May 7, 2008 at 4:38 am
Look at the PIVOT command in BOL, this should do the job.
Piotr
...and your only reply is slàinte mhath
May 7, 2008 at 4:44 am
Piotr Rodak (5/7/2008)
Look at the PIVOT command in BOL, this should do the job.Piotr
UNPIVOT or UNION ALL might be more useful 🙂
There are examples in BOL...
Ryan Randall
Solutions are easy. Understanding the problem, now, that's the hard part.
May 7, 2008 at 4:50 am
Here's an article from the other day with some examples...
http://weblogs.sqlteam.com/jeffs/archive/2008/04/23/unpivot.aspx
Ryan Randall
Solutions are easy. Understanding the problem, now, that's the hard part.
May 7, 2008 at 6:26 am
Thanks for the link to the article, I've not used either PIVOT or UNPIVOT before so I'm impressed with how easy it is to use. However, my query contains joins on other tables and when I add in the UNPIVOT section I start seeing errors relating to multi-part identifiers not being bound.
Should I still be able to join other tables etc?
May 7, 2008 at 6:34 am
David (5/7/2008)
Thanks for the link to the article, I've not used either PIVOT or UNPIVOT before so I'm impressed with how easy it is to use. However, my query contains joins on other tables and when I add in the UNPIVOT section I start seeing errors relating to multi-part identifiers not being bound.Should I still be able to join other tables etc?
Maybe you could add the unpivot query to a derived query instead of directly to your current query.
e.g.
select ...
from ( <your query here> ) myQuery
unpivot ...
Ryan Randall
Solutions are easy. Understanding the problem, now, that's the hard part.
May 7, 2008 at 6:46 am
Perfect! Thanks for your help!
May 7, 2008 at 6:52 am
UNPIVOT or UNION ALL might be more useful 🙂
right :Whistling:
...and your only reply is slàinte mhath
Viewing 8 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply