這是表'VendorItemPricing'。如何在SQL Server中一起使用PIVOT和JOIN?
ItemID VendorName VendorPrice
122 HP 215.13
122 IBM 264.41
122 Microsoft 257.65
我用這個查詢來讓行成列。
Select ItemID,
[HP] As HPPrice ,
[Apple] As ApplePrice,
[Microsoft] As MicrosoftPrice,
[IBM] As IBMPrice from (
select ItemID,VendorName,VendorPrice from VendorItemPricing where ItemID = 122)A
PIVOT(MAX(VendorPrice) FOR VendorName IN ([HP],[Apple],Microsoft,IBM))P
這就是我所期望的輸出。
ItemID HPPrice ApplePrice MicrosoftPrice IBMPrice
122 215.13 NULL 257.65 264.41
這是我的表'MasterItems',我用這個查詢得到下面的結果。
select ItemID, ItemPartNumber, ItemDescription, CreatedDate, InitialPrice from MasterItems where ItemID = 122
這可能是結果。
ItemID ItemPartNumber ItemDescription CreatedDate InitialPrice
122 AB246VB Volt Fuser Kit 2015-05-15 17:17:32.940 215.14
是否可以加入兩個結果並獲得如下結果?
ItemID ItemPartNumber ItemDescription CreatedDate InitialPrice HPPrice ApplePrice MicrosoftPrice IBMPrice
122 AB246VB Volt Fuser Kit 2015-05-15 17:17:32.940 215.14 215.13 NULL 257.65 264.41