动态数据透视表中的行和列总计

Row and column total in dynamic pivot

在 SQL Server 2008 中,我有一个包含 3 列的 table (tblStock):

  • PartCode (NVARCHAR (50))
  • StockQty (INT)
  • Location (NVARCHAR(50))

下面的一些示例数据:

    PartCode    StockQty    Location
   .........   .........    .........
    A              10        WHs-A
    B              22        WHs-A
    A               1        WHs-B
    C              20        WHs-A
    D              39        WHs-F
    E               3        WHs-D
    F               7        WHs-A
    A               9        WHs-C
    D               2        WHs-A
    F              54        WHs-E

如何创建程序得到如下结果?

PartCode    WHs-A   WHs-B   WHs-C   WHs-D   WHs-E   WHs-F   Total
........    .....   .....   .....  ......   .....   .....   .....
A            10       1       9       0       0       0      20
B            22       0       0       0       0       0      22
C            20       0       0       0       0       0      20
D             2       0       0       0       0      39      41
E             0       0       0       3       0       0       3
F             7       0       0       0      54       0      61
Total        61       1       9       3      54      39     167

非常感谢您的帮助,谢谢。

您需要使用 case based aggregation 来旋转数据

要获取 total 行,请使用 union

如果事先不知道 Location 值,则需要构造一个 dynamic query

您也可以使用 pivot 关键字来做同样的事情。

select partCode,
 sum( case when Location='WHs-A' then StockQty
      else 0 end
    ) as 'Whs-A',
 sum( case when Location='WHs-B' then StockQty
      else 0 end
    ) as 'Whs-B',
sum(StockQty) as 'Total'
from tblStock
group by partCode
union all
select 'Total' as 'partCode',
sum( case when Location='WHs-A' then StockQty
    else 0 end ) as 'Whs-A',
sum( case when Location='WHs-B' then StockQty
    else 0 end) as 'Whs-B',
sum(StockQty) as 'Total'
from tblStock

样本TABLE

SELECT * INTO #tblStock
FROM
(
SELECT 'A' PartCode,  10 StockQty, 'WHs-A' Location
UNION ALL
SELECT 'B',   22,  'WHs-A'
UNION ALL
SELECT 'A',   1,   'WHs-B'
UNION ALL
SELECT 'C',   20,  'WHs-A'
UNION ALL
SELECT 'D',   39,  'WHs-F'
UNION ALL
SELECT 'E',   3,   'WHs-D'
UNION ALL
SELECT 'F',   7,   'WHs-A'
UNION ALL
SELECT 'A',   9,   'WHs-C'
UNION ALL
SELECT 'D',   2,   'WHs-A'
UNION ALL
SELECT 'F',   54,  'WHs-E'
)TAB

获取动态旋转的列并将 NULL 替换为 zero

DECLARE @cols NVARCHAR (MAX)

SELECT @cols = COALESCE (@cols + ',[' + Location + ']', '[' + Location + ']')
               FROM (SELECT DISTINCT Location FROM #tblStock) PV 
               ORDER BY Location 
-- Since we need Total in last column, we append it at last
SELECT @cols += ',[Total]'


--Varible to replace NULL with zero
DECLARE @NulltoZeroCols NVARCHAR (MAX)

SELECT @NullToZeroCols = SUBSTRING((SELECT ',ISNULL(['+Location+'],0) AS ['+Location+']' 
FROM (SELECT DISTINCT Location FROM #tblStock)TAB  
ORDER BY Location FOR XML PATH('')),2,8000) 

SELECT @NullToZeroCols += ',ISNULL([Total],0) AS [Total]'

您可以使用 CUBE 来查找行和列的总数,并将 NULL 替换为 Total 以获取从 CUBE 生成的行。

DECLARE @query NVARCHAR(MAX)
SET @query = 'SELECT PartCode,' + @NulltoZeroCols + ' FROM 
             (
                 SELECT 
                 ISNULL(CAST(PartCode AS VARCHAR(30)),''Total'')PartCode, 
                 SUM(StockQty)StockQty , 
                 ISNULL(Location,''Total'')Location              
                 FROM #tblStock
                 GROUP BY Location,PartCode
                 WITH CUBE
             ) x
             PIVOT 
             (
                 MIN(StockQty)
                 FOR Location IN (' + @cols + ')
            ) p
            ORDER BY CASE WHEN (PartCode=''Total'') THEN 1 ELSE 0 END,PartCode' 

EXEC SP_EXECUTESQL @query

结果

注意:如果您想要 NULL 而不是 zero 作为值,请在动态数据透视代码 [=98] 中使用 @cols 而不是 @NulltoZeroCols =]

编辑:

1.仅显示行总计

  • 不要使用代码 SELECT @cols += ',[Total]'SELECT @NullToZeroCols += ',ISNULL([Total],0) AS [Total]'
  • 使用 ROLLUP 而不是 CUBE

2。仅显示列总计

  • 使用代码 SELECT @cols += ',[Total]'SELECT @NullToZeroCols += ',ISNULL([Total],0) AS [Total]'
  • 使用 ROLLUP 代替 CUBE
  • GROUP BY Location,PartCode 更改为 GROUP BY PartCode,Location
  • 而不是 ORDER BY CASE WHEN (PartCode=''Total'') THEN 1 ELSE 0 END,PartCode,使用 WHERE PartCode<>''TOTAL'' ORDER BY PartCode

更新:为 OP

带来 PartName

我正在更新以下查询以添加 PartName 结果。由于 PartName 将添加带有 CUBE 的额外结果,并且为了避免在 ANDOR 条件下混淆,最好将旋转结果与 DISTINCT 中的值结合起来你的来源 table.

DECLARE @query NVARCHAR(MAX)
SET @query = 'SELECT P.PartCode,T.PartName,' + @NulltoZeroCols + ' FROM 
             (                
                 SELECT 
                 ISNULL(CAST(PartCode AS VARCHAR(30)),''Total'')PartCode, 
                 SUM(StockQty)StockQty , 
                 ISNULL(Location,''Total'')Location              
                 FROM #tblStock
                 GROUP BY Location,PartCode
                 WITH CUBE                   
             ) x
             PIVOT 
             (
                 MIN(StockQty)
                 FOR Location IN (' + @cols + ')
            ) p
            LEFT JOIN
            (  
                SELECT DISTINCT PartCode,PartName
                FROM #tblStock  
            )T
            ON P.PartCode=T.PartCode
            ORDER BY CASE WHEN (P.PartCode=''Total'') THEN 1 ELSE 0 END,P.PartCode' 

EXEC SP_EXECUTESQL @query