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 2012 Forums
 Transact-SQL (2012)
 how to change table structure

Author  Topic 

honzo
Starting Member

2 Posts

Posted - 2013-10-27 : 06:39:03
Hello everybody,
i,m new in sql and i have a problem. I was trying to sort it out by myself, but i dont know how. I have obtain a table1 from xml, in this table i'v got two clumns. One is for job description and the other one is for name. Problem is, that I need this table in different format. I need table with 4 columns, where each column is for specific job. How can I do it? I dont want to transpose table because I want adding only names. Please help me.
thank you Honzo.


honzo
Starting Member

2 Posts

Posted - 2013-10-27 : 06:41:20
here is a link for tables, what i need to do. Sorry for previous picture. Honza

[url]http://s21.postimg.org/peiv4sdzr/Untitled.png[/url]
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-10-27 : 14:30:41
This is called pivoting. you can achieve it like this


SELECT MAX(CASE WHEN jobposition = 'Teacher' THEN name END) AS teacher,
MAX(CASE WHEN jobposition = 'programmer' THEN name END) AS programmer,
MAX(CASE WHEN jobposition = 'welder' THEN name END) AS welder,
MAX(CASE WHEN jobposition = 'painter' THEN name END) AS painter
FROM Table


Another way is to PIVOT



SELECT *
FROM Table t
PIVOT (MAX(name) FOR jobposition IN ([teacher],[programmer],[welder],[painter]))p


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -