tags:

views:

435

answers:

4

Hi,

I'm finding some problems with a query that returns the sum of a field from a table for all the records that meet certain conditions. I expected to receive a "No records found' when there were no records, but instead I'm receiving a null result.

SQL> SELECT * FROM DUAL WHERE 1=2;

no rows selected
SQL> SELECT SUM(dummy) FROM DUAL WHERE 1=2;

SUM(DUMMY)
----------


SQL>

Is there any way to not receive any record in that case?

+1  A: 

How about this:

select my_sum
from
(SELECT SUM(dummy) as my_sum FROM DUAL WHERE 1=2)
where
my_sum is not null
Jeffrey L Whitledge
Perfect!! That did the trick!. I know it's a strange question, but I'm working in a data extracion to XML, and it puts everything it gets in the xml, even if its null. The way to avoid that is to no receive anything at all, and that what I was looking for. Thanks!!
Jose L Martinez-Avial
Why would you use a subquery for something like this? That adds unneeded complexity here.
WoLpH
@WoLpH - I use subqueries for everything! Your HAVING solution is a good one too. The optimizer would probably come up with the same thing for both of our queries.
Jeffrey L Whitledge
It would depend on your database server ofcourse, with Oracle or Postgres the optimizer would naturally use the same query plan.With MySQL... I'm not so sure ;)
WoLpH
A: 

You can filter out the null results with having

SELECT SUM(dummy) FROM DUAL WHERE 1=2 HAVING SUM(dummy) IS NOT NULL
WoLpH
Well, that works in that case, but actually my query is more complicated. I have a table with amounts in different currencies, and I use a function to convert each amount to USD, and them sum them. It would be something like thisselect (sum(convert_to_usd(amount,currency)) SCCY_TOTAL from my_tabla where 1=2 having SCCY_TOTAL is not nullThat doesn't work. I get ORA-00904: "SCCY_TOTAL": invalid identifier. So I would need to do something like thisselect (sum(convert_to_usd(amount,currency)) SCCY_TOTAL from my_tabla where 1=2 having (sum(convert_to_usd(amount,currency)) is not nullToo much.
Jose L Martinez-Avial
+2  A: 

No - this is the behavior by design of the RDBMS in use here, Which, to me atleast, makes sense, as you are looking for a sum and not raw data

Itay Moav
I have to agree. I can see arguments for the sum of an empty record set being NULL or 0, but it should be one of the two. It makes little sense for the sum of the empty set to be the empty set.
adharris
If there are no records then the logical result of a sum would be zero. The behavior shown does not make sense.
Jeffrey L Whitledge
@Jeffrey L Whitledge I have no idea what server he is using, but in mysql+sphp it makes sense as it allow you to treat both cases (null and zero) the same programaticly, **and** give you the ability to know when/if no records where found. But then again, I don't know what server he is using. If it was me, I would have opened a ticket in the relevenat company site and see their answer.
Itay Moav
+4  A: 

"I expected to receive a "No records found' when there were no records, but instead I'm receiving a null result."

Then do

SELECT SUM(dummy) FROM DUAL WHERE 1=2 HAVING COUNT(*) > 0

That is, specify that you only want to return a summary where there were rows that were considered.

SELECT SUM(dummy) FROM DUAL WHERE 1=2 HAVING SUM(dummy) IS NOT NULL

is similar, but the COUNT(*) would return a summary row if there were only rows for which dummy was null, while the latter would not.

Gary
+1 For being correct, and showing two ways depending on exactly what is wished for.
Shannon Severance