Subject | Re: Coalesce Help in Stored Procedure |
---|---|
Author | Muthu Annamalai |
Post date | 2004-08-23T18:38:32Z |
--- In firebird-support@yahoogroups.com, Lucas Franzen <luc@r...>
wrote:
I hope I understood.
If there is no matching row then the variable is left untouched and
when using function like sum then it initialize the variable to null
Muthu
wrote:
>coalesce
> Muthu,
>
> >>Yes, but you're using a SUM function here.
> >>
> >>This is NOT the same as a plain select.
> >>The result of SUM itself is initialized with NULL, so the
> >>works. ;-)working
> >>
> >>
> >>Luc.
> >
> >
> > Hi,
> >
> > I still don't understand, I think I can catch after a while
> > with coalesce.found".
>
> The source of the problem is not COALESCE, it's to undesrtand the
> difference between NOTHING FOUND and NULL.
> NULL means NO [UNKNOWN] VALUE, which is totally different to "not
>records
> (Or do you expect a data grid wo show a row of <NULL>s when no
> are found?)return
>
> in your first case you did a:
>
> SELECT COALESCE ( FIELD, 0 ) ...
> FROM ...
> INTO ...
> which resulted in NULLS since your select didn't return any row.
>
> In your second case you did:
>
> SELECT COALESCE(SUM(AMOUNT),0)
> ^^^
> FROM ...
> INTO ...
>
> which resulted in a 0 since:
> SUM is kind of a built-in aggregate function which will always
> NULL if no match is found (or any of the sumands is NULL) so youryour
> COALESCE will return 0 EVEN if no record is found.
>
> It's not that the SELECT isn't evaluated at all, it's just that
> AMOUNT expression will be initialized by using SUM thus COALESCEwill
> get a NULL "input parameter" instead of a "NOTHING" one.Thanks Luc
>
> HTH
>
> Luc.
I hope I understood.
If there is no matching row then the variable is left untouched and
when using function like sum then it initialize the variable to null
Muthu