Query Statement : Having a table "Acct_stat" with column "Trans_type"(Dr or Cr) , "Amount" Pls write a single query which should return sum of "Debit" , "Credit" and "Total Balance" of each account.
Answer : We can use a case statement as best choice for such queries like following with group by Account:
Let's go with following SQL
Drop table Acct_stat;
create table Acct_stat (Acount_No varchar2(10),TRANS_TYPE VARCHAR2(2),AMOUNT NUMBER);
insert into Acct_stat values('1' , 'DR' , 100);
insert into Acct_stat values('1' , 'CR' , 100);
insert into Acct_stat values('1' , 'DR' , 100);
insert into Acct_stat values('2' , 'CR' , 100);
insert into Acct_stat values('2' , 'CR' , 100);
insert into Acct_stat values('2' , 'DR' , 100);
insert into Acct_stat values('3' , 'DR' , 100);
insert into Acct_stat values('3' , 'CR' , 100);
SELECT Acount_No ,
SUM (CASE WHEN Trans_type = 'DR'
THEN AMOUNT ELSE null END) DB_SUM,
SUM (CASE WHEN Trans_type = 'CR'
THEN AMOUNT ELSE null END) CR_SUM,
SUM (AMOUNT) TOT_BAL
FROM ACCT_STAT GROUP BY (Acount_No);
and Result is :
Acount_No DB_SUM CR_SUM TOT_BAL
1 200 100 300
2 100 200 300
3 100 100 200
Cheers
Kapil
This blog is dedicated to share my experience during my development as a purpose of notes and explorer various web / enterprise technologies like JAVA , JEE , Spring ,hybris, Portal , Jquery , RAI , JMS, Weblogic , SSL , Security, CS, MAC< Linux, Windows, Search, IOT, Arduino, Machine Learning, Tips, Angular, Node JS, React, Mac, Windows, Stack, Exception, Error etc. with examples.
Search This Blog
Subscribe to:
Post Comments (Atom)
Popular Posts
-
Recently while installing android SDK , I was getting following error "Unable to elevate" error Solution I tried : 1. R...
-
During git commit -m 'text' we observed error error: invalid object 100644 b1bc4dae98865adf256e130c6bce53bb09d3e93b for 'path...
-
I was getting following problem after copying JAD plugin jar into eclipse plugin folder : java.io.IOException: Cannot run program ...
-
Communication of nodes in SAP commerce(Hybris) environment was failing in cluster with following error: INFO | jvm 1 | main | 2020/0...
No comments:
Post a Comment
Thanks for your comment, will revert as soon as we read it.