Предполагая , что существует какой-то столбец идентификатора, вы можете отменить поворот значений, а затем Повернуть их обратно, используя VALUES
и кросс-таблицу:
CREATE TABLE dbo.YourTable (ID int IDENTITY, --Required to work
n1 tinyint,
n2 tinyint,
n3 tinyint,
n4 tinyint,
n5 tinyint,
n6 tinyint,
[Type] char(2));
GO
INSERT INTO dbo.YourTable (n1,n2, n3, n4, n5, n6, [Type])
VALUES( 6,15,10,11,22,25,'C1'),
( 2, 5, 1,20,21,30,'C1'),
(15,25,18, 4,12,38,'C2'),
(11, 1, 4, 9,24,31,'C2');
GO
SELECT *
FROM dbo.YourTable;
WITH unPvt AS (
SELECT YT.ID,
YT.[Type],
V.Val,
ROW_NUMBER() OVER (PARTITION BY YT.ID ORDER BY V.Val) AS NewPos
FROM dbo.YourTable YT
CROSS APPLY (VALUES(1,YT.n1),
(2,YT.n2),
(3,YT.n3),
(4,YT.n4),
(5,YT.n5),
(6,YT.n6))V(Pos,Val))
SELECT MAX(CASE NewPos WHEN 1 THEN Val END) AS n1,
MAX(CASE NewPos WHEN 2 THEN Val END) AS n2,
MAX(CASE NewPos WHEN 3 THEN Val END) AS n3,
MAX(CASE NewPos WHEN 4 THEN Val END) AS n4,
MAX(CASE NewPos WHEN 5 THEN Val END) AS n5,
MAX(CASE NewPos WHEN 6 THEN Val END) AS n6,
[Type]
FROM unPvt
GROUP BY ID,[Type]
ORDER BY [Type];
GO
DROP TABLE dbo.YourTable;