如何在 SQL 服务器中同时使用 PIVOT 和 JOIN?
How to use PIVOT and JOIN together in SQL Server?
这是table'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
这是我的 table '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
您可以简单地在 PIVOT
的源 table 中执行联接:
Select ItemID, ItemPartNumber, ItemDescription, CreatedDate, InitialPrice,
[HP] As HPPrice,
[Apple] As ApplePrice,
[Microsoft] As MicrosoftPrice,
[IBM] As IBMPrice
from (
select v.ItemID, VendorName, VendorPrice,
ItemPartNumber, ItemDescription, CreatedDate, InitialPrice
from VendorItemPricing as v
left join MasterItems as m on v.ItemID = m.ItemID
where v.ItemID = 122)A
PIVOT(
MAX(VendorPrice)
FOR VendorName IN ([HP],[Apple],Microsoft,IBM)
)P
假设样本数据如您所说 master table 使用 CTE 加入数据透视集结果
declare @table table (Itemid Int,Vendorname varchar(10),VendorPrice DECIMAL(18,2))
insert into @table (Itemid,Vendorname,VendorPrice)
values (122,'HP',125.66),
(122,'microsoft',134.00),
(122,'IBM',124.90)
Declare @master table (Itemid int,Partnumber Varchar(10),Description varchar(20),Created datetime,InitialPrice decimal(18,2))
insert into @master (Itemid,Partnumber,Description,Created,InitialPrice)values (122,'AB246VB','Volt Fuser Kit',GETDATE(),215.14)
;with cte as (
Select Itemid,[HP] As HPPrice ,
[Apple] As ApplePrice,
[microsoft] As microsoftPrice,
[IBM] As IBMPrice from (
select Itemid,Vendorname,VendorPrice from @table where ItemID = 122)A
PIVOT(MAX(VendorPrice) FOR Vendorname IN ([HP],[Apple],[microsoft],[IBM]))P)
select t.Itemid,
t.Partnumber,
t.Description,
t.Created,
t.InitialPrice,
c.HPPrice,c.ApplePrice,
c.IBMPrice,
c.microsoftPrice from @master t,cte c where t.ItemID = 122
SELECT M.*, [HP] As HPPrice,
[Apple] As ApplePrice,
[Microsoft] As MicrosoftPrice,
[IBM] As IBMPrice
FROM MasterItems M
JOIN (
select ItemID,VendorName,VendorPrice from VendorItemPricing where ItemID = 122
)A
PIVOT(MAX(VendorPrice) FOR VendorName IN ([HP],[Apple],Microsoft,IBM)) P
ON M.ItemId = P.ItemId
您可以根据需要将 JOIN 调整为 LEFT JOIN。
这是table'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
这是我的 table '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
您可以简单地在 PIVOT
的源 table 中执行联接:
Select ItemID, ItemPartNumber, ItemDescription, CreatedDate, InitialPrice,
[HP] As HPPrice,
[Apple] As ApplePrice,
[Microsoft] As MicrosoftPrice,
[IBM] As IBMPrice
from (
select v.ItemID, VendorName, VendorPrice,
ItemPartNumber, ItemDescription, CreatedDate, InitialPrice
from VendorItemPricing as v
left join MasterItems as m on v.ItemID = m.ItemID
where v.ItemID = 122)A
PIVOT(
MAX(VendorPrice)
FOR VendorName IN ([HP],[Apple],Microsoft,IBM)
)P
假设样本数据如您所说 master table 使用 CTE 加入数据透视集结果
declare @table table (Itemid Int,Vendorname varchar(10),VendorPrice DECIMAL(18,2))
insert into @table (Itemid,Vendorname,VendorPrice)
values (122,'HP',125.66),
(122,'microsoft',134.00),
(122,'IBM',124.90)
Declare @master table (Itemid int,Partnumber Varchar(10),Description varchar(20),Created datetime,InitialPrice decimal(18,2))
insert into @master (Itemid,Partnumber,Description,Created,InitialPrice)values (122,'AB246VB','Volt Fuser Kit',GETDATE(),215.14)
;with cte as (
Select Itemid,[HP] As HPPrice ,
[Apple] As ApplePrice,
[microsoft] As microsoftPrice,
[IBM] As IBMPrice from (
select Itemid,Vendorname,VendorPrice from @table where ItemID = 122)A
PIVOT(MAX(VendorPrice) FOR Vendorname IN ([HP],[Apple],[microsoft],[IBM]))P)
select t.Itemid,
t.Partnumber,
t.Description,
t.Created,
t.InitialPrice,
c.HPPrice,c.ApplePrice,
c.IBMPrice,
c.microsoftPrice from @master t,cte c where t.ItemID = 122
SELECT M.*, [HP] As HPPrice,
[Apple] As ApplePrice,
[Microsoft] As MicrosoftPrice,
[IBM] As IBMPrice
FROM MasterItems M
JOIN (
select ItemID,VendorName,VendorPrice from VendorItemPricing where ItemID = 122
)A
PIVOT(MAX(VendorPrice) FOR VendorName IN ([HP],[Apple],Microsoft,IBM)) P
ON M.ItemId = P.ItemId
您可以根据需要将 JOIN 调整为 LEFT JOIN。