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 2000 Forums
 Transact-SQL (2000)
 Query to merge table rows into new columns

Author  Topic 

cornall
Posting Yak Master

148 Posts

Posted - 2007-04-25 : 11:07:05
How do i do the following :-


indexA indexB firstName surname
1 1 Dave Davison
1 2 John Johnson
2 1 Bob Robertson
2 2 James Jameson

to

indexA firstName1 surname1 firstname2 surname2
1 Dave Davison John Johnson
2 Bob Robertson James Jameson



If there was 3 resords for indexA I would want to create columns firstName3 and surname3

Hope this makes sense!

cornall
Posting Yak Master

148 Posts

Posted - 2007-04-25 : 11:26:09
A little more info i always want 8 lots of the columns if there is only 2 indexB records for a given indexA then the other 6 have NULL values


indexA firstName1 surname1 firstname2 surname2 firstname3 surname3 firstname4 surname4 firstname5 ........
1 Dave Davison John Johnson NULL NULL NULL NULL NULL
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-04-25 : 11:56:53
This is usually easier to transform at your presentation layer, but if you must do it in T-SQL, you can do something like this:


select indexA,
max(case when indexB=1 then firstname end) as firstname1,
max(case when indexB=1 then surName end) as surName1,
max(case when indexB=2 then firstname end) as firstname2,
max(case when indexB=2 then surName end) as surName2,
..
etc
..
from yourtable
group by indexA





- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page
   

- Advertisement -