SQL 与 Union All 一起旋转

SQL Pivot with Union All

我正在使用 SQL Server 2012。我有 2 个支点(当年 (CY) 与去年 (LY),它们本身运行良好,但想将它们联合起来并将结果放在一起,我该怎么做?我需要让数据看起来像这样:

Cost Centre  Name  May_CY  May_LY  Jun_CY Jun LY.....This_Yr_Total  Last_Yr_Total

这是我的代码:

SELECT        
pvt.*, 
Isnull(pvt.jan_CY, 0) + 
Isnull(pvt.feb_CY, 0) + 
Isnull(pvt.mar_CY, 0) + 
Isnull(pvt.apr_CY, 0) + 
Isnull(pvt.may_CY, 0) + 
Isnull(pvt.jun_CY, 0) + 
Isnull(pvt.jul_CY, 0) + 
Isnull(pvt.aug_CY, 0) + 
Isnull(pvt.sept_CY, 0) + 
Isnull(pvt.oct_CY, 0) + 
Isnull(pvt.nov_CY, 0) +
Isnull(pvt.Dec_CY, 0)
AS  This_Year
FROM            (SELECT        [CostCentre], [CostCentreDesc] AS Name, { fn CONCAT(DATENAME(month, dbo.tblGLS215_2016_2017.AccDate), '_CY') } AS AccMonth, [RecordedAmount]
                          FROM            [tblGLS215_2016_2017]
                          WHERE        Employee <> '') AS s PIVOT (SUM([RecordedAmount]) FOR [AccMonth] IN (May_CY, Jun_CY, Jul_CY, Aug_CY, Sept_CY, Oct_CY, Nov_CY, Dec_CY, Jan_CY, Feb_CY, Mar_CY, Apr_CY)) AS pvt
UNION ALL
SELECT        
pvt.*, 
Isnull(pvt.jan_LY, 0) + 
Isnull(pvt.feb_LY, 0) + 
Isnull(pvt.mar_LY, 0) + 
Isnull(pvt.apr_LY, 0) + 
Isnull(pvt.may_LY, 0) + 
Isnull(pvt.jun_LY, 0) + 
Isnull(pvt.jul_LY, 0) + 
Isnull(pvt.aug_LY, 0) + 
Isnull(pvt.sept_LY, 0) + 
Isnull(pvt.oct_LY, 0) + 
Isnull(pvt.nov_LY, 0) +
Isnull(pvt.Dec_LY, 0)
AS  Last_Year
FROM            (SELECT        [CostCentre], [CostCentreDesc] AS Name, { fn CONCAT(DATENAME(month, dbo.tblGLS215_2015_2016.AccDate), '_LY') } AS AccMonth, [RecordedAmount]
                   FROM            [tblGLS215_2015_2016]
WHERE        Employee <> '') AS s PIVOT (SUM([RecordedAmount]) FOR [AccMonth] IN (May_LY, Jun_LY, Jul_LY, Aug_LY, Sept_LY, Oct_LY, Nov_LY, Dec_LY, Jan_LY, Feb_LY, Mar_LY, Apr_LY)) AS pvt

如有任何帮助,我们将不胜感激。

这是 returns 结果的 'This Year' 的支点之一:

SELECT
    pvt.*,
    Isnull(
        pvt.jan,0)+ Isnull(pvt.feb,0)+ Isnull(pvt.mar,0)+ Isnull(pvt.apr,0)+ Isnull(pvt.may,0)+ Isnull(pvt.jun,0)+ Isnull(pvt.jul,0)+ 
        Isnull(pvt.aug,0)+ Isnull(pvt.sept,0)+ Isnull(pvt.oct,0)+ Isnull(pvt.nov,0) AS This_Year_Total
FROM
    (
        SELECT
            [CostCentre],
            [CostCentreDesc] AS Name,
            CONVERT(
                CHAR(4),
                AccDate,
                100
            ) AS [Month],
            [RecordedAmount]
        FROM
            [tblGLS215_2016_2017]
    ) AS s PIVOT(
        SUM( [RecordedAmount] ) FOR [Month] IN(
            May,
            Jun,
            Jul,
            Aug,
            Sept,
            Oct,
            Nov,
            DEC, Jan, Feb, Mar, Apr)) AS pvt
Select A.[CostCentre], A.Name 
      ,A.[May_CY],B.[May_LY]
      ,A.[Jun_CY],B.[Jun_LY]
      ,A.[Jul_CY],B.[Jul_LY]
      ,A.[Aug_CY],B.[Aug_LY]
      ,A.[Sept_CY],B.[Sept_LY]
      ,A.[Oct_CY],B.[Oct_LY]
      ,A.[Nov_CY],B.[Nov_LY]
      ,A.[Dec_CY],B.[Dec_LY]
      ,A.[Jan_CY],B.[Jan_LY]
      ,A.[Feb_CY],B.[Feb_LY]
      ,A.[Mar_CY],B.[Mar_LY]
      ,A.[Apr_CY],B.[Apr_LY]
      ,A.[This_Year] as This_Yr_Total , B.[Last_Year] as  Last_Yr_Total

From
(
SELECT        
pvt.*, 
Isnull(pvt.jan_CY, 0) + 
Isnull(pvt.feb_CY, 0) + 
Isnull(pvt.mar_CY, 0) + 
Isnull(pvt.apr_CY, 0) + 
Isnull(pvt.may_CY, 0) + 
Isnull(pvt.jun_CY, 0) + 
Isnull(pvt.jul_CY, 0) + 
Isnull(pvt.aug_CY, 0) + 
Isnull(pvt.sept_CY, 0) + 
Isnull(pvt.oct_CY, 0) + 
Isnull(pvt.nov_CY, 0) +
Isnull(pvt.Dec_CY, 0)
AS  This_Year
FROM            (SELECT        [CostCentre], [CostCentreDesc] AS Name, { fn CONCAT(DATENAME(month, dbo.tblGLS215_2016_2017.AccDate), '_CY') } AS AccMonth, [RecordedAmount]
                          FROM            [tblGLS215_2016_2017]
                          WHERE        Employee <> '') AS s PIVOT (SUM([RecordedAmount]) FOR [AccMonth] IN (May_CY, Jun_CY, Jul_CY, Aug_CY, Sept_CY, Oct_CY, Nov_CY, Dec_CY, Jan_CY, Feb_CY, Mar_CY, Apr_CY)) AS pvt
)as A
full outer Join
(
SELECT        
pvt.*, 
Isnull(pvt.jan_LY, 0) + 
Isnull(pvt.feb_LY, 0) + 
Isnull(pvt.mar_LY, 0) + 
Isnull(pvt.apr_LY, 0) + 
Isnull(pvt.may_LY, 0) + 
Isnull(pvt.jun_LY, 0) + 
Isnull(pvt.jul_LY, 0) + 
Isnull(pvt.aug_LY, 0) + 
Isnull(pvt.sept_LY, 0) + 
Isnull(pvt.oct_LY, 0) + 
Isnull(pvt.nov_LY, 0) +
Isnull(pvt.Dec_LY, 0)
AS  Last_Year
FROM            (SELECT        [CostCentre], [CostCentreDesc] AS Name, { fn CONCAT(DATENAME(month, dbo.tblGLS215_2015_2016.AccDate), '_LY') } AS AccMonth, [RecordedAmount]
                   FROM            [tblGLS215_2015_2016]
WHERE        Employee <> '') AS s PIVOT (SUM([RecordedAmount]) FOR [AccMonth] IN (May_LY, Jun_LY, Jul_LY, Aug_LY, Sept_LY, Oct_LY, Nov_LY, Dec_LY, Jan_LY, Feb_LY, Mar_LY, Apr_LY)) AS pvt
) as B
On A.[CostCentre]=B.[CostCentre] and A.Name=B.Name