trouble with ignoring zero sums in group

joe789

Registered User.
Local time
Today, 01:18
Joined
Mar 22, 2001
Messages
154
hey folks,

I am trying to run a query on sql server using the query analyzer ... the query works just fine, but I am attempting to IGNORE any instance where a unique identifier has a sum of units less than zero, and the query doesn't seem to do that:

select provider, fiscal_year, count(distinct ID), sum(finalnetamt), sum(new_quantity) from claimsunion where (stat = 'paid') group by provider, fiscal_year having order by provider, fiscal_year

The above correctly renders to me the proper counts and sum of units and dollars by provider and fiscal year.

Some of the IDs have a 0 sum of (new_quantity) and those I am attempting to ignore from the count, because they wash out/net 0, and should not be counted ... so in an attempt to do that, I am trying a HAVING clause ...

HAVING sum(new_quantity) >0

which renders the same answer and does not remove the IDs that have 0 from the count. I think it is because I don't know how to code that piece that tells sql that it is a having sum(new_quantity) >0 by ID within the group, if that makes sense?

Any help would be greatly appreciated. I know that if I add ID to the group and then use the HAVING sum(new_quantity) >0 it works but it gives me every single ID number and I just want the count not every single ID number.

Thank you very much!!!
 
Is the field actually NULL because Null is not 0 it's an unknown and being an unknown can cause problems, let the field be a value of 0 if it's null using
Nz([new_quantity],0)
then you may be able to us it in a comparison

maybe even with something like

HAVING sum(Nz([new_quantity],0))>0

I'm not positive that will work that way but it may be the right direction...

Cheers!
Goh
 

Users who are viewing this thread

Back
Top Bottom