i have this query where the result shouln't be 0, but it is !
Code: Select all
100 - ( SELECT SUM( "A" - "B" ) FROM "IN" WHERE "ORG" = 'Blabla')
I need an explanation to understand the reason of that unexpected value.
thx in advance
Code: Select all
100 - ( SELECT SUM( "A" - "B" ) FROM "IN" WHERE "ORG" = 'Blabla')
Code: Select all
SELECT 100 - SUM("A" - "B" ) FROM "IN"
Code: Select all
select "Name", "ORG", 100 - ( SELECT SUM( "A" - "B" ) FROM "IN" WHERE "ORG" = 'blabbla') from "Names"
There is your explanation. SUM of a NULL value is NULL.there are no entries in "IN"
No the result is Null, but what's confusing you is that HSQL displays Null as 0.if you use the sub-query ONLY, the result is 0.
Code: Select all
SELECT 100 - COALESCE(SUM("A" - "B" ),0) FROM "IN"
Code: Select all
Select "Name", "ORG",
100 - ( SELECT SUM( "A" - "B" ) FROM "IN" WHERE "ORG" = 'blabbla') COL1,
coalese(cast(100 - ( SELECT SUM( "A" - "B" ) FROM "IN" WHERE "ORG" = 'blabbla') as varchar(10)) COL2, 'IF YOUR READING THIS THEN THE VALUE SHOWN IN COL1 AS ZERO IS ACTUALLY NULL')
from "Names"
Code: Select all
SELECT 100 - COALESCE(SUM( "A" - "B" ),0) FROM "IN"
Code: Select all
SELECT 100 - COALESCE ( SUM( "A" - NULLIF ( "B", '1' ) ), 0 ) FROM "IN"
Villeroy:Villeroy wrote:http://hsqldb.org/doc/guide/builtinfunc ... _functions and you have a local copy of the documentation if you downloaded the full package
I don't get the difference between coalesce and ifnull.
Therefore, it is recommended to use the SQL standard COALESCE.HSQL 2. Documentation: http://www.hsqldb.org/doc/2.0/guide/builtinfunctions-chapt.html wrote:
IFNULL
ISNULL
IFNULL | ISNULL ( <value expr 1>, <value expr 2> )
Returns <value expr 1> if it is not null, otherwise returns <value expr 2>. The type of the return value is
the type of <value expr 1>. Almost equivalent to SQL Standard COALESCE(<value expr 1>, <value
expr 2>) function, but without type modification. (JDBC)