我想要对列进行滚动求和。我创建了以下查询
SET @FinancialIncremental:=0;
SELECT j.ErrorCategory,
COUNT(IF(j.ErrorType = 'P',j.ErrorType,NULL)) AS Financial,
COUNT(IF(j.ErrorType = 'NP',j.ErrorType,NULL)) AS Procedural,
SUM(j.OverPaymentAmount) AS 'Financial Over Paid Amount ($)',
(
SELECT
@FinancialIncremental := @FinancialIncremental + q1.c
FROM
(
(SELECT
COUNT(IF(ErrorType = 'P',ErrorType,NULL)) AS c
FROM DemoScheme.TestTable WHERE IsDeleted = 0 AND ErrorType IN ('P','NP')
GROUP BY ErrorCategory) AS q1
)
) AS 'Financial Incremental'
FROM DemoScheme.TestTable j WHERE IsDeleted = 0 AND ErrorType IN ('P','NP')
GROUP BY ErrorCategory;
但是它给了我一个错误。它给我一个关于“财务增量”子查询的错误
Error Code: 1242. Subquery returns more than 1 row
谁能帮我解决这个问题吗?
最佳答案
其中有一些重复,这使事情变得不必要地复杂化,请尝试此查询。添加时,您可能还需要将 NULL 替换为 0。
SET @FinancialIncremental:=0;
SELECT j.ErrorCategory,
COUNT(IF(j.ErrorType = 'P',j.ErrorType,NULL)) AS Financial,
COUNT(IF(j.ErrorType = 'NP',j.ErrorType,NULL)) AS Procedural,
SUM(j.OverPaymentAmount) AS 'Financial Over Paid Amount ($)',
(@FinancialIncremental := @FinancialIncremental + COUNT(IF(j.ErrorType = 'P',j.ErrorType,0))
AS 'Financial Incremental'
FROM DemoScheme.TestTable j WHERE IsDeleted = 0 AND ErrorType IN ('P','NP')
GROUP BY ErrorCategory;
关于MySql滚动总和问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31668840/