Solved

Case Statement where one column is null

Posted on 2014-12-04
9
242 Views
Last Modified: 2015-01-23
I have two columns LOAN_CT and LOAN_BAL.
In the below statement I am needing to show '0' in LOAN_NO column row  if LOAN_BAL is null else count LOAN_CT.


 SUM( case when (Loan_Bal) > 0 then count(LOAN_CT) else 0 end) as "Count of Loans",
0
Comment
Question by:leezac
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
please post sample data and expected output
0
 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
I don't understand this - >  LOAN_NO column row

what is LOAN_NO? how does it relate to the rest of the sql

what do you mean by "column row" ?

sample data and expected output should help immensely
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
Comment Utility
To get a 0 when there is a null use: NVL(some_column,0)

I'm not understanding what you really want here.
0
 

Accepted Solution

by:
leezac earned 0 total points
Comment Utility
LOAN_NO should be LOAN_CT.

I have values showing in LOAN_CT  1 when there is not a loan balance in LOAN_BAL column.  Should be 0.  Can I add a case statement to show it as 0

LOAN_BAL                  LOAN_CT

0                                          1

I am now updating the table but want to show in results.  May not be able to do - just asking.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
case when loan_bal = 0 then 1 else loan_ct end loan_ct
0
 
LVL 76

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 166 total points
Comment Utility
Maybe this?
 SUM( case when (Loan_Bal) > 0 then 1 else 0 end) as "Count of Loans",
0
 
LVL 1

Assisted Solution

by:jsaun
jsaun earned 167 total points
Comment Utility
From context, I'm guessing that he's trying to get a count of loans where balance is not NULL (BTW, a loan balance should probably never be NULL, only zero.  NULL makes no sense in this context)

If that's the case, then it would be
SELECT COUNT(1)
from LOANS
WHERE Loan_Bal > 0;
0
 
LVL 31

Assisted Solution

by:awking00
awking00 earned 167 total points
Comment Utility
You've provided one simple example but, since you question stated the "case statement where one column is null" even though it may not be realistic for a null LOAN_BAL or even a null LOAN_CT, the possibilities potentially exist that LOAN_BAL could be null, 0 or greater than 0 and LOAN_CT could be null, 0, 1, or greater than 1 so data like the following might be  possible:
LOAN_BAL  LOAN_CT
null               null
null               0
null               1
null               5
0                  null
0                  0
0                  1
0                  5
200              null
200              0
200              1
200              5
Please indicate if any of these possibilities can not exist and what you would expect as results from those possibilities than can.
0
 

Author Closing Comment

by:leezac
Comment Utility
I agree with all of the solutions presented - thank you.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now