hi i have following table and data to get opening and closing balance of every account opening balance required gdate<02-oct-2013 here is my table
create table ledger (account_no varchar2(10),gdate date,debit number(8),credit number(8))
insert into ledger (account_no,gdate,debit,credit) values ('10-0001','01-oct-2013',1000,0);
insert into ledger (account_no,gdate,debit,credit) values ('10-0001','24-oct-2013',0,440);
insert into ledger (account_no,gdate,debit,credit) values ('20-0001','01-oct-2013',3000,0);
insert into ledger (account_no,gdate,debit,credit) values ('30-0001','01-oct-2013',300,0);
insert into ledger (account_no,gdate,debit,credit) values ('20-0001','16-oct-2013',1200,0);
insert into ledger (account_no,gdate,debit,credit) values ('30-0001','17-oct-2013',0,1340);
insert into ledger (account_no,gdate,debit,credit) values ('30-0001','24-oct-2013',500,0);
i need following result
ACCOUNT_NO OPENING DEBIT CREDIT CLOSING
10-0001 1000 0 440 560
20-0001 3000 1200 0 4200
30-0001 300 500 1340 540
You keep changing your requirements, but based on what you shown at the moment, this works:
Which is the same logic I linked to before, when you had different data. SQL Fiddle.
I'm not sure why you're showing your opening balance as positive though; seems wrong when all you have are debits. If that really is what you want then just swap how the balance is calculated:
SQL Fiddle.
This is more of a comment because it doesn't answer the question. Your query simplifies to:
Some notes.
order by
in a subquery or CTE is not useful, unless you are selectingrownum
in the next outer query. If you want the results ordered, then put anorder by
in the outer query.The query itself doesn't make sense. Why would the opening balance come from the previous account number? Usually such terminology is used for dates on a particular account, and you have eliminated all dates in the
group by
clause.