Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Transposing a column

Author  Topic 

a2l2en
Starting Member

11 Posts

Posted - 2007-06-26 : 16:44:14
Is there a way to transpose a table??

I need to transpose the data in the following table. I don't know who created this table or why they did it this way... but it is what it is...

Customer_Contract_table

CustomerID Customer_Name Disposalfee Disp_chrg Engine
1234 Toyota Dealership 6cyl
1234 Toyota Dealership 4cyl
1234 Toyota Dealership Battery $9.00
1234 Toyota Dealership Oil $3.00
1234 Toyota Dealership Tire $2.00
1122 Honda Dealership 6cyl
1122 Honda Dealership 4cyl
5566 CarMax 6cyl
5566 CarMax 4cyl
5566 CarMax Battery $8.00
5566 CarMax Oil $2.00


The output has to be one line per customer... Or look something like this:

CustomerID, customer_name, Battery_Disposal, Battery_disp_charge, Oil_Disposal, Oil_Disposal_charge, Tire_Disposal, Tire_Disposal_charge, 6cylengine, 4cylengine


Is this possible?

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-06-27 : 00:12:36
Read about Cross-tab reports in sql server help file

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

dan18ro
Starting Member

1 Post

Posted - 2007-06-27 : 07:55:41
Try using PIVOT function...I don't remember the parameters but it works...:)
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-06-27 : 08:02:23
Check this: http://msdn2.microsoft.com/en-us/library/ms177410.aspx

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-06-27 : 08:49:35
quote:
Originally posted by dan18ro

Try using PIVOT function...I don't remember the parameters but it works...:)

Yes. PIVOT will do that. I forget that this is posted in 2005 Forum

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

a2l2en
Starting Member

11 Posts

Posted - 2007-06-27 : 12:02:13
Yes! The pivot table worked. Thanks!
Go to Top of Page
   

- Advertisement -