?
Solved

Pivot and Unpivot display

Posted on 2010-08-29
5
Medium Priority
?
1,039 Views
Last Modified: 2012-05-10
I have a table in Sql Server 2008 with these columns in it.
Month, Company, BillableQuantity, BilledAmount, Sales Tax

I need to display the data in this format
Company, Type, June, July, August....

I have attached a sample spreadsheet.  I can not figure out how to write the query to both pivot and unpivot this data.  Please help create this query.
 Pivot.xlsx
0
Comment
Question by:raphil
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 33554352
raphil,

Firstly, you would start with the unpivoting of the data.  You can use a derived table for this:

select [Month], Company, [Type], [Value]
from your_table_name
unpivot ([Value] for [Type] in ([Billable Quantity], [Billed Amount], [Sales Tax])) upvt

Then you would pivot this data by Month which should fit pivot's hard coded column list here as you know the values are January ... December.

select *
from (

select [Month], Company, [Type], [Value]
from your_table_name
unpivot ([Value] for [Type] in ([Billable Quantity], [Billed Amount], [Sales Tax])) upvt

) data
pivot (sum([Value]) for [Month] in ([January],...,[December])) pvt

Hope that helps.

Kevin
0
 

Author Comment

by:raphil
ID: 33554385
The type of column "BilledAmount" conflicts with the type of other columns specified in the UNPIVOT list.The type of column "BilledAmount" conflicts with the type of other columns specified in the UNPIVOT list.
0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 2000 total points
ID: 33554455
Yes, sorry, that is because in most systems quantity (at least at the sellable level) will be whole numbers as customers tend to not buy partial parts *smile*; therefore, you will have to cast all the columns to same data type like decimal or money before doing the unpivot.

;with cte
as
(
   select [Month], Company
        , convert(decimal(14,4), [BillableQuantity]) AS BQ
        , convert(decimal(14,4), [BilledAmount] as BA
        , convert(decimal(14,4), [SalesTax]) as ST
   from your_table_name
), upvt
as
(
   select [Month], Company, [Type], [Value]
   from cte
   unpivot ([Value] for [Type] in ([BQ], [BA], [ST])) u
)
-- final select (pvt)
select *
from upvt
pivot (sum([Value]) for [Month] in ([January],...,[December])) p
;

Open in new window

0
 

Author Closing Comment

by:raphil
ID: 33554508
Awesome!!!
Thanks a ton!  That worked like a breaze!

All points!
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 33554509
Glad to have helped.
Best regards and happy coding,

Kevin
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question