Question : Calculate Total for the same transaction

Hi All,

I have structure data below:

TrsNo  SeqNo Debit  Credit
A          1            10      
A          2            10
A          3                        30
A          4                        10
B          1            20
B          2                         30

I want to make it become:

TrsNo  SeqNo  Debit   Credit   TotalDebit   TotalCredit
A          1              10                           20               40
A          2              10                           20               40
A          3                        30                 20               40
A          4                        10                 20               40
B          1              20                           20               30
B          2                         30                20               30

How could I solve this problem ?

Thank you.

 

Answer : Calculate Total for the same transaction

try this
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
SELECT t.*,
       SUM(debet) OVER (PARTITION BY notransaksi) totaldebit,
       SUM(kredit) OVER (PARTITION BY notransaksi) totalcredit
FROM (SELECT a.notransaksi,
             CASE WHEN right(a.tipetransaksi, 1) = 'D' THEN SUM(a.nilaitransaksi) ELSE 0.00 END
                 AS totaldebet,
             CASE
                 WHEN right(a.tipetransaksi, 1) = 'K' THEN SUM(b.nilaitransaksi * a.nilaikurs)
                 ELSE 0.00
             END
                 AS totalkredit,
             CASE WHEN right(a.tipetransaksi, 1) = 'D' THEN b.nilaitransaksi ELSE 0.00 END AS debet,
             CASE
                 WHEN right(a.tipetransaksi, 1) = 'K' THEN b.nilaitransaksi * a.nilaikurs
                 ELSE 0.00
             END
                 AS kredit
      FROM thbank2010 as a INNER JOIN tdbank2010 as b ON a.notransaksi = b.notransaksi
      WHERE (a.notransaksi = 'BK 015/9966/01/10')
      GROUP BY a.notransaksi, a.tipetransaksi, b.nilaitransaksi, a.nilaikurs) as t
Random Solutions  
 
programming4us programming4us