raphil
asked on
Pivot and Unpivot display
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
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
ASKER
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Awesome!!!
Thanks a ton! That worked like a breaze!
All points!
Thanks a ton! That worked like a breaze!
All points!
Glad to have helped.
Best regards and happy coding,
Kevin
Best regards and happy coding,
Kevin
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])
Hope that helps.
Kevin