Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Case Statement where one column is null

Posted on 2014-12-04
9
Medium Priority
?
271 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 74

Expert Comment

by:sdstuber
ID: 40481093
please post sample data and expected output
0
 
LVL 74

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 77

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
Give Your Engineering Team a Productivity Boost

Learn why container technology is so powerful and how it can provide your team with productivity gains and other benefits.

 

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 74

Expert Comment

by:sdstuber
ID: 40481145
case when loan_bal = 0 then 1 else loan_ct end loan_ct
0
 
LVL 77

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 664 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 668 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 668 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

Not sure which OpenStack Certification to get?

So you’ve realized you might want to get certified in OpenStack, but you’re not sure what the benefits might be or even which one you should take. You know there are several certification courses you can choose from, but how do you know which one is right for you?

Question has a verified solution.

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

Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

715 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