Skip to Main Content
  • Questions
  • ORACLE SQL Question sum credits and debits

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Suman.

Asked: February 04, 2016 - 7:28 am UTC

Last updated: April 09, 2018 - 10:03 am UTC

Version: 10g

Viewed 10K+ times! This question is

You Asked

I have a transaction table called TRANSACTION DETAIL....

TRANSACTION DETAIL

CNN TYPE AMT DATE

C1 C 1000 10-Jan-16
C2 C 1200 10-Jan-16
C3 C 2000 11-Jan-16
C4 D 1000 12-Jan-16
C3 D 2000 12-Jan-16
C2 C 1100 12-Jan-16

I need total credit and debit for those 3 paricular dates.
REQUIRED OUTPUT

DATE TOTAL DEBIT TOTAL CREDIT
10-Jan-16 0 2200
11-Jan-16 2000 0
12-Jan-16 3000 100

Please help me out on this to get the query...



Thanks,
Sam

and Chris said...

You're looking for a "group by then pivot". Calculate the sum for each date and type:

create table t (
  cnn varchar2(2), type varchar2(1), amt integer, dt date
);

insert into t values ('C1','C', 1000, '10-Jan-16');
insert into t values ('C2','C', 1200, '10-Jan-16');
insert into t values ('C3','C', 2000, '11-Jan-16');
insert into t values ('C4','D', 1000, '12-Jan-16');
insert into t values ('C3','D', 2000, '12-Jan-16');
insert into t values ('C2','C', 1100, '12-Jan-16');

commit;

select dt, type, sum(amt) sm from t
group  by dt, type;

DT                   T         SM
-------------------- - ----------
10-JAN-0016 00:00:00 C       2200
12-JAN-0016 00:00:00 D       3000
11-JAN-0016 00:00:00 C       2000
12-JAN-0016 00:00:00 C       1100


Then pivot the results of this to get credit and debit as columns. As you're on 10g, you'll have to do this the long way:

select dt, 
       nvl(max(case when type = 'C' then sm end), 0) credit_total, 
       nvl(max(case when type = 'D' then sm end), 0) debit_total
from (
  select dt, type, sum(amt) sm from t
  group  by dt, type
)
group by dt
order  by 1;

DT                   CREDIT_TOTAL DEBIT_TOTAL
-------------------- ------------ -----------
10-JAN-0016 00:00:00         2200           0
11-JAN-0016 00:00:00         2000           0
12-JAN-0016 00:00:00         1100        3000


On 11g you can do this instead:

select * from (
  select dt, type, sum(amt) sm from t
  group  by dt, type
)
pivot (
  sum(sm) as total for (type) in ('C' as credit, 'D' as debit)
);

DT                   CREDIT_TOTAL DEBIT_TOTAL
-------------------- ------------ -----------
10-JAN-0016 00:00:00         2200            
11-JAN-0016 00:00:00         2000            
12-JAN-0016 00:00:00         1100        3000

Rating

  (3 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Match_Recognize

Rajeshwaran, Jeyabal, February 04, 2016 - 12:53 pm UTC

Well, in 12c you could do this.

rajesh@ORA12C> set feedback off
rajesh@ORA12C> drop table t purge;
rajesh@ORA12C> create table t(cnn varchar2(3),type varchar2(1),amt int,dt date);
rajesh@ORA12C> insert into t values('C1','C',1000,to_date('10-Jan-2016','dd-mon-yyyy'));
rajesh@ORA12C> insert into t values('C2','C',1200,to_date('10-Jan-2016','dd-mon-yyyy'));
rajesh@ORA12C> insert into t values('C3','C',2000,to_date('11-Jan-2016','dd-mon-yyyy'));
rajesh@ORA12C> insert into t values('C4','D',1000,to_date('12-Jan-2016','dd-mon-yyyy'));
rajesh@ORA12C> insert into t values('C3','D',2000,to_date('12-Jan-2016','dd-mon-yyyy'));
rajesh@ORA12C> insert into t values('C2','C',1100,to_date('12-Jan-2016','dd-mon-yyyy'));
rajesh@ORA12C> commit;
rajesh@ORA12C> select *
  2  from t
  3  match_recognize(
  4    order by dt
  5    measures
  6      dt dts ,
  7      sum( decode(type,'C',amt,0)) credit_total,
  8      sum( decode(type,'D',amt,0)) debit_total
  9    one row per match
 10    pattern( a b* )
 11      define b as prev(dt) = dt);

DTS         CREDIT_TOTAL DEBIT_TOTAL
----------- ------------ -----------
10-JAN-2016         2200           0
11-JAN-2016         2000           0
12-JAN-2016         1100        3000

rajesh@ORA12C>

Chris Saxon
February 04, 2016 - 5:40 pm UTC

Yep, you could do that too.

A reader, February 05, 2016 - 12:09 am UTC


select dt, sum(case when type='C' then amt END) Credit_Total, sum(case when type='D' then amt END) as Debit_Total
from t
group  by dt;


rony, March 28, 2018 - 5:39 pm UTC

select * from(select vchno,ntr,sum(amnt) sm from d_tran2
group by vchno,ntr)
pivot
(
sum(sm) as total for(ntr) in ('C' as credit,'D' as debit)
);


*how can i use this code in apex 5.1.I want to show the debit amount in the details table debit display field and the credit amount in the details table credit display field.
thanks
Chris Saxon
April 09, 2018 - 10:03 am UTC

I'm no APEX expert. And I'm not sure precisely what you're trying to here. So I can't really help...

Try asking on the APEX forum. With lots more detail about what you're trying to do!

https://community.oracle.com/community/database/developer-tools/application_express