如何在数据透视表上有多个列?

How to have multiple columns on Pivot tables?

我的 sql 服务器中有一个 table,名为 VendorItemPricing。

select * from VendorItemPricing  where ItemID = 122

这个查询会给我以下结果。

ItemID    VendorName    VendorPrice    UpdatedDate                ObsoleteItem
122       HP            215.13         2015-05-15 11:55:49.983    0
122       IBM           264.41         2015-05-15 11:56:04.990    0
122       Microsoft     257.65         2015-05-15 11:56:23.963    0

我想在列而不是行上显示 HP、Apple 等,所以我使用了 Pivot tables

Select ItemID,
[HP] As HPUpdatedDate,
[Apple] As AppleUpdatedDate,
[Microsoft] As MicrosoftUpdatedDate,
[IBM] As IBMUpdatedDate from (
select Itemid,Vendorname,UpdatedDate from VendorItemPricing where ItemID = 122)A
PIVOT(
MAX(UpdatedDate) FOR Vendorname IN ([HP],[Apple],Microsoft,IBM)
)P

以上查询结果如:

ItemID  HPUpdatedDate               AppleUpdatedDate    MicrosoftUpdatedDate        IBMUpdatedDate
122     2015-05-15 11:55:49.983     NULL                2015-05-15 11:56:23.963     2015-05-15 11:56:04.990

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

Select ItemID,
[HP] As HPObsoleteItem,
[Apple] As AppleObsoleteItem,
[Microsoft] As MicrosoftObsoleteItem,
[IBM] As IBMObsoleteItem  from (
select Itemid,Vendorname,CAST(ObsoleteItem AS TINYINT) AS INTColumn from VendorItemPricing where ItemID = 122)A
PIVOT(
MAX(INTColumn) FOR Vendorname IN ([HP],[Apple],Microsoft,IBM)
)P

以上查询结果如:

ItemID      HPObsoleteItem      AppleObsoleteItem       MicrosoftObsoleteItem   IBMObsoleteItem
122         0                   NULL                    0                       0

是否有可能得到如下结果?很抱歉这个冗长的问题。任何帮助都感激不尽。

ItemID  HPUpdatedDate               AppleUpdatedDate    MicrosoftUpdatedDate        IBMUpdatedDate              HPPrice     ApplePrice  MicrosoftPrice  IBMPrice        HPObsoleteItem      AppleObsoleteItem   MicrosoftObsoleteItem   IBMObsoleteItem
122     2015-05-15 11:55:49.983     NULL                2015-05-15 11:56:23.963     2015-05-15 11:56:04.990     215.13      NULL        257.65          264.41          0                   NULL                0                       0

您可以使用子查询或 CTE,然后根据 ItemID 连接所有三个查询以获取所有必要的字段:

;WITH q1
AS (
    SELECT ItemID
        ,[HP] AS HPUpdatedDate
        ,[Apple] AS AppleUpdatedDate
        ,[Microsoft] AS MicrosoftUpdatedDate
        ,[IBM] AS IBMUpdatedDate
    FROM (
        SELECT Itemid
            ,Vendorname
            ,UpdatedDate
        FROM VendorItemPricing
        WHERE ItemID = 122
        ) A
    PIVOT(MAX(UpdatedDate) FOR Vendorname IN (
                [HP]
                ,[Apple]
                ,Microsoft
                ,IBM
                )) P
    )
    ,q2
AS (
    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
    )
    ,q3
AS (
    SELECT ItemID
        ,[HP] AS HPObsoleteItem
        ,[Apple] AS AppleObsoleteItem
        ,[Microsoft] AS MicrosoftObsoleteItem
        ,[IBM] AS IBMObsoleteItem
    FROM (
        SELECT Itemid
            ,Vendorname
            ,CAST(ObsoleteItem AS TINYINT) AS INTColumn
        FROM VendorItemPricing
        WHERE ItemID = 122
        ) A
    PIVOT(MAX(INTColumn) FOR Vendorname IN (
                [HP]
                ,[Apple]
                ,Microsoft
                ,IBM
                )) P
    )
SELECT q1.ItemID
    ,q1.HPUpdatedDate
    ,q1.AppleUpdatedDate
    ,q1.MicrosoftUpdatedDate
    ,q1.IBMUpdatedDate
    ,q2.HPPrice
    ,q2.ApplePrice
    ,q2.MicrosoftPrice
    ,q2.IBMPrice
    ,q3.HPObsoleteItem
    ,q3.AppleObsoleteItem
    ,q3.MicrosoftObsoleteItem
    ,q3.IBMObsoleteItem
FROM q1
JOIN q2 ON q1.ItemID = q2.ItemID
JOIN q3 ON q1.ItemID = q3.ItemID