Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
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|DiagCode1234|1|34541234|2|56841234|3|7584Into:1234|3454|5684|7584and 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 yourTablePIVOT( MAX(DiagCode) FOR Diagseq IN ([1],[2],[3])) P