0
create table #Contact(
LoanNumber int,
ContactType varchar(10),
CompanyName varchar(10),
CompanyPhone varchar(10),
CONSTRAINT PK PRIMARY KEY (LoanNumber,ContactType)
)
Insert into #Contact
values (1,'Appriaser','Yige King','11'),
(1,'AssetOwner','gqqnbig','22')
這是我的表格。 ContactTypes只是Appriaser和AssetOwner。樞軸輸出兩個值
我能得到這樣
LoanNumber AppraiserCompanyName AppraiserCompanyPhone AssertOwnerCompanyName AssertOwnerCompanyPhone
----------------------------------------------------------------------------------------------------
6103339 YigeKing 11 gqqnbig 22
表我設法寫這
select LoanNumber,
CompanyNamePT.Appriaser as AppriaserCompanyName, CompanyNamePT.AssetOwner as AssetOwnerCompanyName
--CompanyPhonePT.Appriaser as AppriaserCompanyPhone, CompanyPhonePT.AssetOwner as AssetOwnerCompanyPhone
from (
select #contact.LoanNumber, #contact.ContactType, #contact.CompanyName
from #contact
) as c
pivot (max(c.CompanyName) for c.ContactType in (Appriaser,AssetOwner)) as CompanyNamePT
--pivot (max(c.CompanyPhone) for c.ContactType in ([Appriaser],[AssetOwner])) as CompanyPhonePT
它輸出的公司名稱,但如果我取消註釋兩行拿到電話號碼它會拋出語法錯誤。
我該如何讓它工作?理想情況下,我想使用樞軸,因爲我學習了它。
側的問題,我怎麼能完全限定LoanNumber?我試着做'c.LoanNumber',但它不起作用。 – Gqqnbig
我的意思是在'select LoanNumber'中,爲什麼我不能寫'select c.LoanNumber'? – Gqqnbig
@LoveRight您可以選擇A.LoanNumber我分配了A的別名,但將其更改爲C是一個小問題。一會兒 –