合并特定输出的2个SQL数据透视查询

时间:2014-12-28 16:01:01

标签: sql-server merge pivot union-all

我正在使用SQL Server 2014,我需要合并2个透视查询,以便获得特定输出(详见下文)。

这是Pivot Query 1:

SELECT  [PropertyCode],

cast(cast([DECEMBER 2014]/(((SELECT rooms FROM HotelInventory c WHERE c.propertycode=pvttable.propertycode)*31.)/100.) as decimal (9,0)) as varchar) + '%' as 'Dec 2014',
cast(cast([JANUARY 2015]/(((SELECT rooms FROM HotelInventory c WHERE c.propertycode=pvttable.propertycode)*31.)/100.) as decimal (9,0)) as varchar) + '%' as 'Jan 2015',
cast(cast([FEBRUARY 2015]/(((SELECT rooms FROM HotelInventory c WHERE c.propertycode=pvttable.propertycode)*28.)/100.) as decimal (9,0)) as varchar) + '%' as 'Feb 2015'


FROM   (SELECT [PropertyCode],
       [MTH],
       [ROOM NIGHTS]

FROM   HOLDINGS
WHERE DateOfDeparture > '2014-12-01') a


   PIVOT (Sum([ROOM NIGHTS])
         FOR [MTH] IN ([DECEMBER 2014],
                       [JANUARY 2015],
                       [FEBRUARY 2015])) AS PVTTABLE

Pivot Query 1的输出如下:

PropertyCode    Dec 2014    Jan 2015    Feb 2015
     A             75%        60%         35%
     B             85%        78%         22%
     C             69%        86%         38%

这是Pivot Query 2:

SELECT  [PropertyCode],

cast(cast([DECEMBER 2014]/(((SELECT rooms FROM HotelInventory c WHERE c.propertycode=BUDGETPVTTABLE.propertycode)*31.)/100.) as decimal (9,0)) as varchar) + '%' as 'Budget Dec2014',
cast(cast([JANUARY 2015]/(((SELECT rooms FROM HotelInventory c WHERE c.propertycode=BUDGETPVTTABLE.propertycode)*31.)/100.) as decimal (9,0)) as varchar) + '%' as 'Budget Jan2015',
cast(cast([FEBRUARY 2015]/(((SELECT rooms FROM HotelInventory c WHERE c.propertycode=BUDGETPVTTABLE.propertycode)*28.)/100.) as decimal (9,0)) as varchar) + '%' as 'Budget Feb2015'

FROM   (SELECT [PropertyCode],
           [MTH],
           [RN]
    FROM   Budget1415) b

   PIVOT (Sum([RN])
         FOR [MTH] IN ([NOVEMBER 2014],
                       [DECEMBER 2014],
                       [JANUARY 2015],
                       [FEBRUARY 2015])) AS BUDGETPVTTABLE

ORDER BY [PropertyCode]

Pivot Query 2的输出如下:

PropertyCode    Budget Dec2014    Budget Jan2015    Budget Feb2015
     A             95%                 70%              60%
     B             90%                 89%              85%
     C             75%                 91%              80%

我已经使用" UNION ALL"合并了Pivot Query 1和Pivot Query 2。我得到以下输出:

PropertyCode    Dec2014    Jan2015    Feb2015
     A             95%       70%        60%
     A             75%       60%        35%
     B             90%       89%        85%
     B             85%       78%        22%
     C             75%       91%        80%
     C             69%       86%        38%

它看起来不错,但我追求这个特殊的设计:

PropertyCode    Budget Dec2014  Dec 2014    Budget Jan2015  Jan 2015    Budget Feb2015  Feb 2015
   A                 95%          75%           70%            60%          60%            35%
   B                 90%          85%           89%            78%          85%            22%
   C                 75%          69%           91%            86%          80%            38%

1 个答案:

答案 0 :(得分:0)

如何做join

with q1 as (<query1 here without order by),
     q2 as (<query2 here without order by>)
select q1.PropertyCode,
       q1.Dec2014 as Budget_Dec2014, q2.Dec2014,
       q1.Dec2014 as Budget_Jan2015, q2.Jan2015,
       q1.Dec2014 as Budget_Feb2015, q2.Feb2015
from q1 join
     q2
     on q1.PropertyCode = q2.PropertyCode;