gpt4 book ai didi

mysql - mysql select 中的小计和总计不正确

转载 作者:行者123 更新时间:2023-11-29 18:21:50 28 4
gpt4 key购买 nike

我已经设法在下面的 sql select 中分离出负数和正数,并且我正在尝试获取表图片中所示的小计和总计,第三列是正确的结果应该是什么。

enter image description here

下面是我使用过的sql select,但它返回上表第2列中的结果,并且不正确。如何获取第 3 列中的小计和总计结果

感谢和问候

SELECT * FROM
(SELECT
party AS "Account",
sum(debit-credit) AS "Balance"
FROM `tabGL Entry`
WHERE party_type ="Customer" AND MONTH(posting_date) = MONTH(Now()) -1
GROUP BY party WITH ROLLUP
HAVING sum(debit-credit) < 0
UNION ALL
SELECT
party AS "Account",
sum(debit-credit) AS "Balance"
FROM `tabGL Entry`
WHERE party_type ="Customer" AND MONTH(posting_date) = MONTH(Now()) -1
GROUP BY party WITH ROLLUP
HAVING sum(debit-credit) > 0
) rec

UNION ALL

SELECT NULL AS party, sum(dep) AS Balance
FROM (
SELECT SUM(credit-debit) AS dep
FROM `tabGL Entry`
UNION ALL
SELECT SUM(credit-debit) AS nodep
FROM `tabGL Entry`
) AS recb
GROUP BY party WITH ROLLUP

最佳答案

使用此语法可以防止错误附加GROUP BY:

( SELECT ... GROUP BY ... )
UNION ALL
( SELECT ... GROUP BY ... )

对比

( SELECT ...  )
UNION ALL
( SELECT ... )
GROUP BY ...

“小计”一词从何而来?我在查询中没有看到它。

我怀疑内部WITH ROLLUP被扔掉了。如果删除它们会发生什么?

关于mysql - mysql select 中的小计和总计不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46508738/

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