DECLARE @T TABLE (Customer VARCHAR(5), sn INT, val1 INT, val2 INT, Val3 INT)INSERT INTO @TSELECT 'A', 1, 31, 54, NULLUNION ALL SELECT 'A', 2, 98, 21, 65UNION ALL SELECT 'A', 3, 44, 67, 11;WITH MyVals AS ( SELECT Customer, sn, n=1, val=val1 FROM @T UNION ALL SELECT Customer, sn, 2, val2 FROM @T UNION ALL SELECT Customer, sn, 3, val3 FROM @T )SELECT Customer ,val1_1=MAX(CASE WHEN sn=1 AND n=1 THEN val END) ,val1_2=MAX(CASE WHEN sn=1 AND n=2 THEN val END) ,val1_3=MAX(CASE WHEN sn=1 AND n=3 THEN val END) ,val2_1=MAX(CASE WHEN sn=2 AND n=1 THEN val END) ,val2_2=MAX(CASE WHEN sn=2 AND n=2 THEN val END) ,val2_2=MAX(CASE WHEN sn=2 AND n=3 THEN val END) ,val3_1=MAX(CASE WHEN sn=3 AND n=1 THEN val END) ,val3_2=MAX(CASE WHEN sn=3 AND n=2 THEN val END) ,val3_3=MAX(CASE WHEN sn=3 AND n=3 THEN val END) FROM MyValsWHERE val IS NOT NULLGROUP BY Customer