1

我想通过 mysql 存储过程中的交叉表汇总总计和小计。我会在我的脚本中添加小计。

        SET @VARBRANCH='
            MAX(IF(Branch =''A'',`data`, NULL)) AS ''A'',
            MAX(IF(Branch =''B'',`data`, NULL)) AS ''B''';

            DROP TEMPORARY TABLE IF EXISTS TBL_ACBR;
            SET @VARBRANCH=CONCAT('CREATE TEMPORARY TABLE TBL_ACBR SELECT Prtype,SUM(data) AS Total,  '
            ,@VARBRANCH,
            ' FROM(
            SELECT Prtype, Branch, `data` FROM TBLGETDATAALL_1 GROUP BY  Branch,Prtype) t
            GROUP BY Prtype;');
            PREPARE stmacbr FROM @VARBRANCH;
            EXECUTE stmacbr;
            DEALLOCATE PREPARE stmacbr; 

            SELECT * FROM TBL_ACBR;

它上面的脚本显示如下:

Prtype Total  A  B
JAM     5     3  2  
Jim     5     3  3

我需要的结果如下:

Prtype Total  A  B
JAM     5     3  2  
Jim     5     3  3
Total   10    6   5
4

1 回答 1

0

只需将您的最终选择更改为工会,包括总数。

改变:

SELECT * FROM TBL_ACBR

至:

SELECT Prtype, Total, A, B FROM TBL_ACBR
UNION
SELECT 'Total' AS Prtype, SUM(Total), SUM(A), SUM(B) FROM TBL_ACBR
于 2013-06-02T08:52:58.370 回答