您应该能够将以下内容与 UNPIVOT 函数一起使用:
select loanNumber,
amount,
amounttype
from #temp
unpivot
(
amount
for amounttype in (AmntType1, AmntType2, AmntType3)
) unp;
See SQL 摆弄演示 http://sqlfiddle.com/#!3/d41d8/29227.
或者因为您使用的是 SQL Server 2008 R2,所以也可以使用以下方式编写CROSS APPLY
:
select loannumber,
amount,
amounttype
from #temp
cross apply
(
values
('AmntType1', AmntType1),
('AmntType2', AmntType2),
('AmntType3', AmntType3)
) c (amounttype, amount);
See SQL 摆弄演示 http://sqlfiddle.com/#!3/d41d8/29228