如何将连接 SELECT 语句的多个列汇总到一个组合行中?

How to ROLLUP multiple columns that are joined SELECT statements into a single combined row?

我正在从 table [数据].[FCNP_Log] 中创建一个视图,其中包含多个不同 'Production Areas' 的多个不同属性的计数。我需要的是在 'Production Area' 列下显示 'All' 的附加行,并且每个列中的所有其他行的总和。我几乎得到了我需要的东西,但下面的代码并没有完全满足我的需要。

SELECT 
        ISNULL(total.[Production Area], 'All') AS [Production Area]
    ,   COALESCE(total.[Total], 0) AS [Total]
    ,   COALESCE(inprocess.[In Process], 0) AS [In Process]
    ,   COALESCE(closed.[Closed], 0) AS [Closed]
    ,   COALESCE(nofurtheraction.[No Further Action], 0) AS [No Further Action]
    ,   COALESCE([60day].[60 Day], 0) AS [60 Day]
    ,   COALESCE(dir.[DIR], 0) AS [DIR]
    ,   COALESCE([8d].[8D], 0) AS [8D]
FROM
(SELECT 
        [DEPARTMENT] AS 'Production Area'
    ,   COUNT([FCNP]) AS 'Total'
FROM [Data].[FCNP_Log]
GROUP BY [DEPARTMENT] WITH ROLLUP) total

FULL OUTER JOIN
(SELECT 
        [DEPARTMENT] AS 'Production Area'
    ,   COUNT([FCNP]) AS 'In Process'
FROM [Data].[FCNP_Log]
WHERE [FCNPStatus] = 'In Process'
GROUP BY [DEPARTMENT] WITH ROLLUP) inprocess

ON
total.[Production Area] = inprocess.[Production Area]

FULL OUTER JOIN
(SELECT
        [DEPARTMENT] AS 'Production Area'
    ,   COUNT(FCNP) AS 'Closed'
FROM [Data].[FCNP_Log]
WHERE [FCNPStatus] = 'Closed'
GROUP BY [DEPARTMENT] WITH ROLLUP) closed

ON
total.[Production Area] = closed.[Production Area]

FULL OUTER JOIN
(SELECT
        [DEPARTMENT] AS 'Production Area'
    ,   COUNT([FCNP]) AS 'No Further Action'
FROM [Data].[FCNP_Log]
WHERE [Long_TermCANoFurtherAction] = 1
GROUP BY [DEPARTMENT] WITH ROLLUP) nofurtheraction

ON
total.[Production Area] = nofurtheraction.[Production Area]

FULL OUTER JOIN
(SELECT
        [DEPARTMENT] AS 'Production Area'
    ,   COUNT([FCNP]) AS '60 Day'
FROM [Data].[FCNP_Log]
WHERE [Long_Term60Day] = 1
GROUP BY [DEPARTMENT] WITH ROLLUP) [60day]

ON
total.[Production Area] = [60day].[Production Area]

FULL OUTER JOIN
(SELECT
        [DEPARTMENT] AS 'Production Area'
    ,   COUNT([FCNP]) AS 'DIR'
FROM [Data].[FCNP_Log]
WHERE [Long_TermCADIR] = 1
GROUP BY [DEPARTMENT] WITH ROLLUP) dir

ON
total.[Production Area] = dir.[Production Area]

FULL OUTER JOIN
(SELECT
        [DEPARTMENT] AS 'Production Area'
    ,   COUNT([FCNP]) AS '8D'
FROM [Data].[FCNP_Log]
WHERE [Long_TermCA8D] = 1
GROUP BY [DEPARTMENT] WITH ROLLUP) [8d]

ON
total.[Production Area] = [8d].[Production Area]

此代码为我提供了以下内容 table: http://i.imgur.com/LjSvZSK.png

如您所见,我的 ROLLUP 正确地添加了列,但它们是交错的,我需要将所有 'All' 行合并为一行。

我已经尝试将整个代码块选择为 "t1" 并在末尾添加一个额外的 GROUP BY t1.[Production Area] WITH ROLLUP,但它给了我错误。

我觉得可能有一个简单的解决方案,但我想不出来。感谢任何帮助。

编辑: 我确实找到了一种方法来获得我想要的结果。我删除了所有 WITH ROLLUP 并将 WITH CountTable AS ( 放在上面的代码之前,然后是以下代码:

)


SELECT * FROM CountTable
UNION
SELECT
        'All'                               AS [Production Area]
    ,   SUM(CountTable.[Total])             AS [Total]
    ,   SUM(CountTable.[In Process])        AS [In Process]
    ,   SUM(CountTable.[Closed])            AS [Closed]
    ,   SUM(CountTable.[No Further Action]) AS [No Further Action]
    ,   SUM(CountTable.[60 Day])            AS [60 Day]
    ,   SUM(CountTable.[DIR])               AS [DIR]
    ,   SUM(CountTable.[8D])                AS [8D]
FROM CountTable

编辑: 我确实想出了一种方法来获得我想要的结果。我删除了所有 WITH ROLLUP 并将 WITH CountTable AS ( 放在上面的代码之前,然后是以下代码:

)


SELECT * FROM CountTable
UNION
SELECT
        'All'                               AS [Production Area]
    ,   SUM(CountTable.[Total])             AS [Total]
    ,   SUM(CountTable.[In Process])        AS [In Process]
    ,   SUM(CountTable.[Closed])            AS [Closed]
    ,   SUM(CountTable.[No Further Action]) AS [No Further Action]
    ,   SUM(CountTable.[60 Day])            AS [60 Day]
    ,   SUM(CountTable.[DIR])               AS [DIR]
    ,   SUM(CountTable.[8D])                AS [8D]
FROM CountTable