I am using SQL Server 2005 for my application. I have a table in my stored procedure which has two columns, C1 and C2. I want to transpose this table such that the values of
This is probably a case where dynamic sql is your friend. Assuming your base table's name is "Transpose":
--Get a Unique List of values from C1 --> These will be our column headers
DECLARE @Unique TABLE (C1 VARCHAR(25), fUsed BIT DEFAULT 0);
INSERT INTO @Unique (C1) SELECT DISTINCT C1 FROM Transpose;
DECLARE @TransSQL NVARCHAR(4000);
DECLARE @ColID NVARCHAR(25);
SET @TransSQL = N'SELECT '
--Loop over unique C1 values and construct the select statement
SELECT @ColID = (SELECT TOP 1 C1 FROM @Unique WHERE fUSed = 0);
WHILE @@ROWCOUNT <> 0 AND @ColID IS NOT NULL
BEGIN
SET @TransSQL = @TransSQL + 'CASE C1 WHEN ' + '''' + @ColID + '''' + ' THEN C2 ELSE NULL END AS ' + @ColID + ', '
--Update flag in table so we don't use this field again
UPDATE u SET fUsed = 1 FROM @Unique u WHERE C1 = @ColID;
SELECT @ColID = (SELECT TOP 1 C1 FROM @Unique WHERE fUSed = 0);
END
--Remove Trailing comma and add FROM clause
DECLARE @SQL NVARCHAR(4000)
SET @SQL = LEFT(@TransSQL,LEN(@TransSQL) -1) + ' FROM Transpose'
--For debugging purposes
PRINT @SQL;
--Execute the dynamic sql
EXEC sp_executesql @SQL;
This won't achieve the exact layout your question described because their isn't a key field to group the results by. Instead, the output will look like the following:
M1 M2 M3
U1 NULL NULL
U2 NULL NULL
U3 NULL NULL
NULL U4 NULL
NULL U5 NULL
NULL NULL U6
If your base table has a key field the query can be modified slightly to group the results by the key field and probably come a bit closer to your stated goal for the resulting data.