以下是我尝试过的简单代码。只需运行,看看它是否符合您的要求!
declare @Tbl1 table(Category Varchar(10), Value1 Int, Value2 Int, Value3 Int, Value4 Int, Value5 Int)
insert into @Tbl1(Category, Value1, Value2, Value3, Value4, Value5)
Values ('Hot', 12, 23, 43, 5, 6)
insert into @Tbl1(Category, Value1, Value2, Value3, Value4, Value5)
Values ('Warm', 41, 28, 4, 45, 16)
insert into @Tbl1(Category, Value1, Value2, Value3, Value4, Value5)
Values ('Cold', 1, 3, 543, 15, 26)
select * from @Tbl1
Declare @TblUP Table(Category Varchar(100), Value Int, [Types] Varchar(10))
Insert into @TblUP(Category, Value, Types)
select * from @Tbl1 unpivot ([values] for Types in (Value1, Value2, Value3, Value4, Value5)) as UNPIV
select * from @TblUP
Select Types, Hot, Warm, Cold
From
(Select Types, Category, Value from @TblUP) as TUP
PIVOT (SUM(Value) for [Category] in ([Hot], [Warm], [Cold]))
as PT