本文介紹了如何將列標題轉換為貸款編號的行的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
我陷入了逆向旋轉.我在下面有一個像#temp 這樣的表.使用 sql server 2008 r2
I am stuck in unpivoting. I have a table like #temp below. Using sql server 2008 r2
Select LoanNumber = 2000424385
,[AmntType1] = 120.32
,[AmntType2] = 131.52
,[AmntType3] = 142.36
into #temp
從 #temp 中選擇 *
select * from #temp
上面的表格只有一行,我想要下面的三行
Above table has only one row and i want three rows as below
LoanNumber Amount AmountType
2000424385 120.32 AmntType1
2000424385 131.52 AmntType2
2000424385 120.32 AmntType1
推薦答案
您應該能夠通過 UNPIVOT 函數使用以下內容:
You should be able to use the following with the UNPIVOT function:
select loanNumber,
amount,
amounttype
from #temp
unpivot
(
amount
for amounttype in (AmntType1, AmntType2, AmntType3)
) unp;
請參閱SQL Fiddle with Demo.
或者因為您使用的是 SQL Server 2008 R2,這也可以使用 CROSS APPLY
編寫:
Or because you are using SQL Server 2008 R2, this can also be written using CROSS APPLY
:
select loannumber,
amount,
amounttype
from #temp
cross apply
(
values
('AmntType1', AmntType1),
('AmntType2', AmntType2),
('AmntType3', AmntType3)
) c (amounttype, amount);
參見SQL Fiddle with Demo
這篇關于如何將列標題轉換為貸款編號的行的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!