2016-09-19 138 views
0

我被卡住了像圖片這樣的計算函數,我只能通過借方進行求和。根據記錄類型計算總和

的歌手是他的系統

Saldo =如果位置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' 

回答

0

這樣的事情?

/* 
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:[email protected] + (
      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    | 
+------+------+-------+--------+--------------+-------------------+--------------------+