Solved

Case Statement where one column is null

Posted on 2014-12-04
9
251 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
ID: 40481093
please post sample data and expected output
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 40481098
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)
ID: 40481103
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
ID: 40481139
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 73

Expert Comment

by:sdstuber
ID: 40481145
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
ID: 40481154
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
ID: 40481248
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 32

Assisted Solution

by:awking00
awking00 earned 167 total points
ID: 40483062
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
ID: 40566019
I agree with all of the solutions presented - thank you.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

862 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

23 Experts available now in Live!

Get 1:1 Help Now