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
 convert table

Author  Topic 

MLS
Starting Member

1 Post

Posted - 2010-04-16 : 11:04:21
Hi. I have a large table, This is a sample data set from table1. How can I get table 2 from table1 to have one unique record for each person, please note that column headers between Table1 and table 2 are different:
Table 1
Name Color Age Enrolled
Jo red 24 Y
Jo blue 24 Y
Ray red 31 Y
MIke blue 28 N

Table 2
Name Red Blue Age Enrolled
Jo red blue 24 Y
Ray red 31 Y
Mike blue 28 N
Thanks.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-19 : 04:31:46
[code]
INSERT INTO Table2
SELECT Name,
MAX(CASE WHEN Color='red' THEN Color ELSE NULL END),
MAX(CASE WHEN Color='blue' THEN Color ELSE NULL END),
Age,
Enrolled
FROM Table1
GROUP BY Name,Age,Enrolled
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -