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
 Vertical to Horizontal

Author  Topic 

boehnc
Starting Member

15 Posts

Posted - 2012-01-17 : 15:25:39
I tried searching but couldn't find exactly what I'm looking for. To scale down my table for this example, I need to get:

PatientID|Diagseq|DiagCode
1234|1|3454
1234|2|5684
1234|3|7584

Into:

1234|3454|5684|7584

and then create the row so that each patient has 24 columns available in case the patient has that many diagnosis codes.

Thanks for any help!

ehorn
Master Smack Fu Yak Hacker

1632 Posts

Posted - 2012-01-17 : 15:30:51
Hello boehnc,

Perhaps the PIVOT operator will help you here. Something like:


SELECT
PatientID,
[1] AS [DiagCode1],
[2] AS [DiagCode2],
[3] AS [DiagCode3]
--...
FROM
yourTable
PIVOT
( MAX(DiagCode) FOR Diagseq IN ([1],[2],[3])) P
Go to Top of Page
   

- Advertisement -