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
 General SQL Server Forums
 New to SQL Server Programming
 change horizontal to vertical

Author  Topic 

vijays3
Constraint Violating Yak Guru

354 Posts

Posted - 2010-12-02 : 08:18:32

Hi all

I am in trouble because i have to change the display of the data
in y table .please suggest me how to do this.My table containing data
in below order data1 ,data2 ,data3 are column names

data 1 data2 data3
apple1 orange1 1
apple2 orange1 2
apple2 orange3 3


but i need to change it in below format

1 2 3
orange1 orange1 orange3
apple1 apple2 apple2


please advise me on this

ahmeds08
Aged Yak Warrior

737 Posts

Posted - 2010-12-03 : 01:14:12
you can do it by using pivot function
Go to Top of Page

ahmeds08
Aged Yak Warrior

737 Posts

Posted - 2010-12-03 : 01:16:39
replace this query with your columns
SELECT [xxx],
[yyy],
[zzz]
FROM ( SELECT [emp_name],
[emp_id]
FROM employee
) p PIVOT ( max(emp_id)
FOR [emp_name] IN ([xxx],[yyy],[zzz])
) AS pvt
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-12-07 : 03:04:16
For unknown number of values, use
http://beyondrelational.com/blogs/madhivanan/archive/2008/08/27/dynamic-pivot-in-sql-server-2005.aspx

Madhivanan

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

- Advertisement -