Solved

Case Statement where one column is null

Posted on 2014-12-04
9
254 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
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.

 

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
 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Select only the top record in a left join 13 34
Query still returning duplicates 5 27
Oracle Insert not working 10 22
ORA-02288: invalid OPEN mode 2 11
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
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.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines

813 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

11 Experts available now in Live!

Get 1:1 Help Now