【发布时间】:2015-07-29 23:05:51
【问题描述】:
这是表'VendorItemPricing'。
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
【问题讨论】:
标签: sql-server join sql-server-2012 pivot-table