gpt4 book ai didi

php - 根据记录类型计算总和

转载 作者:行者123 更新时间:2023-11-29 20:19:20 27 4
gpt4 key购买 nike

我被如图的计算功能卡住了,只能借方求和。

歌手就是他的系统

Saldo = IF THE POSITION 列 1 列 2 贷方 借方 借方 所以结果 - 贷方,贷方 IF 列 1 列 2 贷方,那么结果是贷方 - 贷方,贷方 IF 列 1 列 2 借方,贷方 + 借方 所以结果等如图

results

这是我的代码

select 
no,
tanggal,
O.debet,
(select sum(debet) from tbl_jurnal_umum where no <= O.no) 'Saldo'
from tbl_jurnal_umum O
where nama_perkiraan = 'kas'

最佳答案

类似这样的吗?

/*
DROP TABLE T;
CREATE TABLE T(NAME VARCHAR(3),ID INT, TRTYPE VARCHAR(1), AMOUNT INT);
INSERT INTO T VALUES
('abc',1,'D','500'),('abc',1,'C','50'),('abc',1,'C','10'),('abc',1,'C','40'),
('abc',1,'D','500'),('abc',1,'C','100');
*/

SELECT S.NAME,S.ID,S.DEBIT,S.CREDIT,s.runtot runningtotal,
CASE WHEN S.DEBIT <> '' THEN S.RUNTOT ELSE '' END AS DebitRunningTotal,
CASE WHEN S.DEBIT = '' THEN S.RUNTOT ELSE '' END AS CreditRunningTotal
from
(
SELECT T.NAME,T.ID,
CASE WHEN T.TRTYPE = 'D' THEN T.AMOUNT ELSE '' END AS DEBIT,
CASE WHEN T.TRTYPE = 'C' THEN T.AMOUNT ELSE '' END AS CREDIT,
@RUNTOT:=@RUNTOT + (
CASE WHEN TRTYPE = 'D' THEN AMOUNT
ELSE AMOUNT * -1
END )
AS RUNTOT
FROM (SELECT @RUNTOT:=0) RT, T T
) s

+------+------+-------+--------+--------------+-------------------+--------------------+
| NAME | ID | DEBIT | CREDIT | runningtotal | DebitRunningTotal | CreditRunningTotal |
+------+------+-------+--------+--------------+-------------------+--------------------+
| abc | 1 | 500 | | 500 | 500 | |
| abc | 1 | | 50 | 450 | | 450 |
| abc | 1 | | 10 | 440 | | 440 |
| abc | 1 | | 40 | 400 | | 400 |
| abc | 1 | 500 | | 900 | 900 | |
| abc | 1 | | 100 | 800 | | 800 |
+------+------+-------+--------+--------------+-------------------+--------------------+

关于php - 根据记录类型计算总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39570275/

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