SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL query returning Null
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

stoolpidgeon
Starting Member

United Kingdom
28 Posts

Posted - 06/15/2013 :  09:28:25  Show Profile  Reply with Quote
The following is supposed to check the month in the date column, and then check the denominator in a calculation I want to perform. As long as the denominator isn't zero it should perform the calculation and finally average the result of the calculation over account numbers thus producing a cross tab query.

For a reason unbeknownst to me it's returning Null for all account numbers:

Select AccountNo,
avg(case month(importdate) when 1 then (case (stxmonthfce + stxmonthcase) when 0 then null else
(stxmonthcase / (stxmonthfce + stxmonthcase)) end) end) as January,
avg(case month(importdate) when 2 then (case (stxmonthfce + stxmonthcase) when 0 then null else
(stxmonthcase / (stxmonthfce + stxmonthcase)) end) end) as February
From publications.tblchristask
Group by AccountNo

James K
Flowing Fount of Yak Knowledge

3575 Posts

Posted - 06/15/2013 :  10:22:01  Show Profile  Reply with Quote
quote:
Originally posted by stoolpidgeon

The following is supposed to check the month in the date column, and then check the denominator in a calculation I want to perform. As long as the denominator isn't zero it should perform the calculation and finally average the result of the calculation over account numbers thus producing a cross tab query.

For a reason unbeknownst to me it's returning Null for all account numbers:

Select AccountNo,
avg(case month(importdate) when 1 then (case (stxmonthfce + stxmonthcase) when 0 then null else
(stxmonthcase / (stxmonthfce + stxmonthcase)) end) end) as January,
avg(case month(importdate) when 2 then (case (stxmonthfce + stxmonthcase) when 0 then null else
(stxmonthcase / (stxmonthfce + stxmonthcase)) end) end) as February
From publications.tblchristask
Group by AccountNo

This can happen if some of the columns are null and others are null in your addition operations. For example, "stxmonthfce + stxmonthcase". Change that to ISNULL(stxmonthfce,0) + ISNULL(stxmonthcase,0).
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000