将 Total 列添加到 Dynamic SQL pivot

Posted

技术标签:

【中文标题】将 Total 列添加到 Dynamic SQL pivot【英文标题】:Add Total column to Dynamic SQL pivot 【发布时间】:2021-01-29 19:31:05 【问题描述】:

那里有很多这类问题,但找不到与我正在寻找的内容相匹配的任何内容。

我正在使用另一篇文章中运行良好的代码,但我想对其进行修改以在表格底部添加一个总计行。

上一篇让创作者获得荣誉的帖子是:SQL Server dynamic pivot with multiple columns

我当前的表格如下所示:

Work_Group Avg 01/01/2021 01/01/2021 01/01/2021 ect...
Blue 5 2 5 8
Green 5 2 5 8

目标:

Work_Group Avg 01/01/2021 01/01/2021 01/01/2021 ect...
Blue 5 2 5 8
Green 5 2 5 8
TOTAL 10 4 10 16

当前代码是:

DECLARE @colsEAST AS NVARCHAR(MAX);
DECLARE @pivotCountEAST AS NVARCHAR(MAX);
DECLARE @sqlEAST AS NVARCHAR(MAX);

SELECT @colsEAST = ISNULL(@colsEAST + ', ', '') + QUOTENAME(Load_Date)
FROM (SELECT DISTINCT Load_Date FROM ##CE_tmpEast_ALL) AS Load_Date

SET @pivotCountEAST = N'SELECT Work_Group, ' + @colsEAST +'
FROM (SELECT Work_Group, Load_Date, Count FROM ##CEtmp_East_ALL) AS Count
PIVOT(SUM(Count) FOR Load_Date IN (' + @colsEAST + ')) AS pvt';

SET @sqlEAST = '; WITH MTD_CountPivot AS (
'+@pivotCountEAST+'
),

MTD_Pivot_Tables AS (
  SELECT Work_Group, AVG(Count) AS [AVG Count]
  FROM ##CEtmp_East_ALL
  GROUP BY Work_Group
)
SELECT MTD_CountPivot.Work_Group,  MTD_Pivot_Tables.[AVG Count], ' + @colsEAST + '
FROM
MTD_CountPivot

INNER JOIN MTD_Pivot_Tables ON MTD_CountPivot.Work_Group = MTD_Pivot_Tables.Work_Group';

EXEC sp_executesql @sqlEAST;

任何帮助将不胜感激!!!

谢谢

【问题讨论】:

在输入表中添加一个并集?与分组(在一个虚拟列上)+总和 【参考方案1】:

您可以将GROUPING SETS ((mcp.Work_Group),()) 添加到当前查询以获取小计。为此,需要创建另一个参数@colsEAST2,以便将生成的列标题日期列表保存为

SUM([2001-01-01]) AS [2001-01-01],SUM([2001-01-02]) AS [2001-01-02],SUM([2001-01-03]) AS [2001-01-03]

@colsEAST 一起持有

[2001-01-01],[2001-01-02],[2001-01-03]

DECLARE @colsEAST       AS NVARCHAR(MAX);
DECLARE @colsEAST2      AS NVARCHAR(MAX);
DECLARE @pivotCountEAST AS NVARCHAR(MAX);
DECLARE @sqlEAST        AS NVARCHAR(MAX);

SELECT @colsEAST = ISNULL(@colsEAST + ', ', '') + QUOTENAME(Load_Date)
  FROM (SELECT DISTINCT Load_Date 
          FROM ##CE_tmpEast_ALL) AS Load_Date;    
SELECT @colsEAST2 = ISNULL(@colsEAST2 + ', ', '') 
                + ' SUM('+QUOTENAME(Load_Date)+') AS '+QUOTENAME(Load_Date)
  FROM (SELECT DISTINCT Load_Date 
          FROM ##CE_tmpEast_ALL) AS Load_Date;    
SET @pivotCountEAST = N'SELECT Work_Group, ' + @colsEAST2 +'
                          FROM (SELECT Work_Group, Load_Date, Count 
                                  FROM ##CE_tmpEast_ALL) AS Count
                         PIVOT(SUM(Count) FOR Load_Date IN (' + @colsEAST + ')) AS pvt
                         GROUP BY Work_Group';                             
SET @sqlEAST = '; WITH MTD_CountPivot AS (
'+@pivotCountEAST+'
), MTD_Pivot_Tables AS (
  SELECT Work_Group, AVG(Count) AS [AVG Count]
    FROM ##CE_tmpEast_ALL
   GROUP BY Work_Group
)
SELECT CASE WHEN GROUPING(mcp.Work_Group) = 0 
            THEN mcp.Work_Group 
            ELSE ''TOTAL'' 
             END AS Work_Group,
       SUM(mpt.[AVG Count]) AS Avg, 
       ' + @colsEAST2+ '
  FROM MTD_CountPivot mcp
  JOIN MTD_Pivot_Tables mpt 
    ON mcp.Work_Group = mpt.Work_Group
 GROUP BY GROUPING SETS ((mcp.Work_Group),())';    
EXEC sp_executesql @sqlEAST;

Demo

【讨论】:

以上是关于将 Total 列添加到 Dynamic SQL pivot的主要内容,如果未能解决你的问题,请参考以下文章

如何将额外的列添加到包含前一列百分比的选择语句

将行添加到 SQL 查询结果,其中包含某些列的总数

带有连接的 Oracle SQL 更新列

SQL将计算列添加到表中

将 varbinary 更新到现有 SQL 列(添加到现有图像)?

SQL 临时将计数列添加到返回的结果集中