添加时所有产品的MS SQL Server 2008中的动态数据透视表中的行和列总计我的问题是行总数和列总数

时间:2016-10-11 11:10:08

标签: sql sql-server sql-server-2008 tsql pivot

我有这个存储过程:

USE [Baxe]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[CreateTable]
AS
BEGIN
  DECLARE @cols AS nvarchar( max) , 
   @query AS nvarchar( max);
    SET @cols = STUFF(( SELECT DISTINCT ',' + QUOTENAME( bmarke)

         FROM subbuyer FOR XML PATH( '' ) , TYPE ).value( '.' , 'NVARCHAR(MAX)') , 1 , 1 , '');
        SET @query = 'SELECT bdate, ' + @cols + ' from ( select bdate, bprice as q , bmarke from subbuyer ) x
        pivot ( SUM(q)for bmarke in (' + @cols + ') ) p ';
       select @cols =  STUFF((SELECT distinct ',' + QUOTENAME(convert(CHAR(10), bmarke, 120)) 
                    from subbuyer
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'') 
      PRINT @query;  
       PRINT @cols; 
     EXECUTE ( @query );
   end

这给了我这个输出:

image

TOTAL的最后一行是我需要添加的内容。我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

将您的查询放入CTE或临时表中并使用GROUP BY ROLLUP

;WITH cte AS (
--your query here
)

SELECT  ISNULL(bdate,'TOTAL') bdate, 
        SUM(Total) Total, 
        SUM(ACCESSORIES) ACCESSORIES, 
        SUM(NBB) NBB, 
        SUM(UNDERWARE) UNDERWARE
FROM cte
GROUP BY ROLLUP (bdate)

输出:

bdate       Total   ACCESSORIES NBB     UNDERWARE
13/04/2016  7000    7000        NULL    NULL
14/04/2016  3000    NULL        NULL    3000
15/04/2016  3000    NULL        NULL    3000
16/04/2016  3000    NULL        3000    NULL
17/04/2016  23000   14000       6000    3000
TOTAL       39000   21000       9000    9000

修改#1

根据您提供的样本。您需要使用带SUM函数的列名添加变量:

DECLARE @sumcols nvarchar(max)
SET @sumcols = STUFF(( SELECT DISTINCT ', SUM(' + QUOTENAME( bmarke)+') as ' + QUOTENAME(bmarke)
         FROM subbuyer FOR XML PATH( '' ) , TYPE ).value( '.' , 'NVARCHAR(MAX)') , 1 , 1 , '');

并更改您的动态查询,因此您之前的查询将位于子查询中:

--Forgot about two quotes around TOTAL
SET @query = 'SELECT ISNULL(bdate,''TOTAL'') bdate, '+@sumcols + ' FROM (
    SELECT bdate, ' + @cols + ' from ( select bdate, bprice as q , bmarke from subbuyer ) x
    pivot ( SUM(q)for bmarke in (' + @cols + ') ) p 
    ) t GROUP BY ROLLUP (bdate)';

希望这有帮助。

修改#2

要添加TOTAL列使用:

DECLARE @pluscols nvarchar(max)
SET @pluscols = STUFF(( SELECT DISTINCT '+' + QUOTENAME( bmarke)
         FROM subbuyer FOR XML PATH( '' ) , TYPE ).value( '.' , 'NVARCHAR(MAX)') , 1 , 1 , '') + ' as [TOTAL]';

并更改您的动态查询,因此您之前的查询将位于子查询中:

--Forgot about two quotes around TOTAL
SET @query = 'SELECT ISNULL(bdate,''TOTAL'') bdate, '+@sumcols + ', SUM([TOTAL]) as [TOTAL] FROM (
    SELECT bdate, ' + @cols + ', ' + @pluscols + ' from ( select bdate, bprice as q , bmarke from subbuyer ) x
    pivot ( SUM(q)for bmarke in (' + @cols + ') ) p 
    ) t GROUP BY ROLLUP (bdate)';

答案 1 :(得分:0)

i like to make this picture

这是我的制片人

use baxe
DECLARE @cols AS nvarchar( max) , 
   @query AS nvarchar( max);
    SET @cols = STUFF(( SELECT DISTINCT ',' + QUOTENAME( bmarke)     
         FROM subbuyer FOR XML PATH( '' ) , TYPE ).value( '.' , 'NVARCHAR(MAX)') , 1 , 1 , '');
       SET @query = 'SELECT ISNULL(bdate,''TOTAL'') bdate, '+@cols + ' FROM (SELECT bdate, ' + @cols + ' from ( select bdate, bprice as q , bmarke from subbuyer ) x
        pivot ( SUM(q)for bmarke in (' + @cols + ') ) p;
    )  GROUP BY ROLLUP (bdate)';

  PRINT (@query )
    PRINT (@cols)
    EXEC  (@query)