Solved

Case Statement where one column is null

Posted on 2014-12-04
9
268 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
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…

695 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