gpt4 book ai didi

mysql - 卡住总和小计mysql交叉表

转载 作者:行者123 更新时间:2023-11-30 23:17:38 25 4
gpt4 key购买 nike

我想在 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

最佳答案

只需将您的最终选择更改为包含总数的并集即可。

改变:

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

关于mysql - 卡住总和小计mysql交叉表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16881188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com