我想在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
答案 0 :(得分: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