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)
 Rows to Columns

Author  Topic 

chrisg229
Starting Member

12 Posts

Posted - 2007-10-30 : 12:52:16
I need to take these following rows and transfer them into columns:
<table>
<tr>
<td>NAME_Col</td><td>Type_Col</td>
</tr>
<tr>
<td>'Des 1'</td><td>P</td>
</tr>
<tr>
<td>'Des 2'</td><td>S</td>
</tr>
<tr>
<td>'Des 3'</td><td>S</td>
</tr>
<tr>
<td>'Des 4'</td><td>S</td>
</tr>
</table>


like this:

<table>
<tr>
<td>Name_P_Col</td><td>Name_S1_Col</td><td>Name_S2_Col</td><td>Name_S3_Col</td>
</tr>
<tr>
<td>'Des1'</td><td>'Des2'</td><td>'Des3'</td><td>'Des4'</td>
</tr>
</table>



The first column(Name_P) should be where type = 'P' like so:
case WHEN Type = 'P' then NAME end AS Name_P_Col


The other 3 where Type = 'S' can go to any one of the Name_Sx_Col but I'm not sure how to do this when the only rule is that type = 'S'


Hope I was clear enough. Any suggestions?

Thanks in advance.
Chris

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2007-10-30 : 12:57:38
search these forums for PIVOT.

Dinakar Nethi
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-10-31 : 02:28:18
Also read about Cross-tab reports in sql server help file

Madhivanan

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

- Advertisement -