【问题标题】:Merge 2 SQL pivot queries for a specific ouput为特定输出合并 2 个 SQL 数据透视查询
【发布时间】:2015-02-25 00:02:40
【问题描述】:

我正在使用 SQL Server 2014,我有 2 个需要合并的数据透视查询,以便获得特定的输出(下面将详细介绍)。

这是枢轴查询 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

透视查询 1 的输出如下:

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

这是枢轴查询 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%

【问题讨论】:

    标签: sql-server merge pivot union-all


    【解决方案1】:

    来个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;
    

    【讨论】:

    • 谢谢,但我收到以下错误消息:“Msg 319, Level 15, State 1, Line 3 关键字'with'附近的语法不正确。如果此语句是公用表表达式,则为 xmlnamespaces 子句或更改跟踪上下文子句,前面的语句必须以分号结束。"
    • 对不起...我也在 q2 旁边写了“WITH”子句!编辑我的代码,我会回复你! :-)
    • 我将您的代码更改如下,它们运行良好:选择 q1.PropertyCode, q1.[Dec 2014] as 'Dec 2014', q2.[Budget Dec 2014], q1.[Jan 2015] as 'Jan 2015', q2.[Budget Jan 2015], q1.[Feb 2015] as 'Feb 2015', q2.[Budget Feb 2015] 非常感谢,戈登!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-14
    • 2011-06-18
    相关资源
    最近更新 更多