pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: John Fabiani <johnf@jfcomputer.com>
To: pgsql-sql@postgresql.org
Subject: Re: something simple but I can't
Date: Sat, 29 Jan 2011 07:14:22 -0800
Message-ID: <201101290714.22366.johnf@jfcomputer.com> (raw)
In-Reply-To: <ii02it$dcm$1@reversiblemaps.ath.cx>
References: <201101281716.04534.johnf@jfcomputer.com>
	<ii02it$dcm$1@reversiblemaps.ath.cx>

On Friday, January 28, 2011 07:46:37 pm Jasen Betts wrote:
> On 2011-01-29, John Fabiani <johnf@jfcomputer.com> wrote:
> > Hi guys,
> > I trying to return a 0.00 from a function it there are no records found
> > else return the amount.
> > 
> >   select sum(aropen_paid) into _paidamt FROM public.aropen where
> > 
> > aropen_applyto is not null and (aropen_applyto = $1) ;
> > 
> >         IF (FOUND) THEN
> >         
> >           RETURN _paidamt ;
> > 	
> > 	END IF;
> > 	
> >       RETURN 0.00 ;
> > 
> > But all I get is either a blank or the amount paid.  What am I doing
> > wrong??? Johnf
> 
>  how many rows does the query return when no rows match the where?
>  It returns 1  that looks like ( NULL ).
>  it return 1 row, which is more than zero thus FOUND is TRUE.
> 
> you can fix your function  by changing the IF to
> 
>  IF _paidamt IS NOT NULL
> 
> 
> but if you change the sum to
> 
>   coalesce(sum(aropen_paid),0.00)
> 
> you can do the task more simply like this:
> 
>  create or replace function danmeans_getpayments(text)
>  returns numeric as
>  $BODY$
>    select coalesce(sum(aropen_paid),0.00) FROM public.aropen where
>   aropen_applyto is not null and (aropen_applyto = $1) ;
>  $BODY$
>    LANGUAGE 'sql' ;
Thanks to all who replied.  All of your suggestions would work and it should 
have been obvious but it wasn't.

Johnf



view thread (3+ messages)

Message-ID: <201101290714.22366.johnf@jfcomputer.com>
Permalink:  ../201101290714.22366.johnf@jfcomputer.com/
Also on:    postgresql.org/message-id/201101290714.22366.johnf@jfcomputer.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: johnf@jfcomputer.com
  Subject: Re: something simple but I can't
  In-Reply-To: <201101290714.22366.johnf@jfcomputer.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox