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.
Dear All,I have the following tableid type week1002 B w11002 B w31001 B w41003 B w11003 B w21003 B w31003 B w4i want to produce following (col is fixed week-1 to week-4)id type week-1 week-2 week-3 week-41001 B NA NA NA w41002 B w1 NA w3 NA1003 B w1 w2 w3 w41001 has only w4 though 1002 and 1003 have w1,w3 and w1,w2,w3,w4 respectively.Best Regards,Abdullah Al Mamun
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts
Posted - 2011-04-12 : 07:47:49
You can do this using PIVOT, like this:
SELECT ID, [Type], ISNULL(W1, 'NA') AS [Week-1], ISNULL(W2, 'NA') AS [Week-2], ISNULL(W3, 'NA') AS [Week-3], ISNULL(W4, 'NA') AS [Week-4]FROM YourTable PIVOT(MAX([week]) FOR [week] IN ([w1], [w2], [w3], [w4])) P