From: | elein <elein(at)sbcglobal(dot)net> |
---|---|
To: | Jean-Christian Imbeault <jc(at)mega-bucks(dot)co(dot)jp>, pgsql-general <pgsql-general(at)postgresql(dot)org> |
Subject: | Re: PL/PGSQL question |
Date: | 2003-04-10 01:03:55 |
Message-ID: | 200304100107.h3A17i4N377322@pimout3-ext.prodigy.net |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
Coalesce is a great function. It enables you to
code up IF X IS NOT NULL THEN X ELSE Y END in one
easy function. There are coalesce functions for
most datatypes and you can write your own for any
other types.
This will do what you want.
... select into total_sales coalesce( sum(price), 0) ...
elein(at)varlena(dot)com
On Thursday 16 January 2003 22:09, Jean-Christian Imbeault wrote:
> I wrote the following plpgsql function. The problem I have is that if no
> rows are found my function returns NULL whereas it should be returning 0.
>
> Where have I erred?
>
> create or replace function zoqoo_total_sales(integer) returns integer as '
> declare total_sales integer := 0;
> begin
> select into total_sales sum(price)
> from invoice_li, invoices WHERE
> shop_id=$1 AND not invoice_li.cancelled
> AND shipped AND invoices.id=invoice_id
> AND not invoices.cancelled AND payment_rcvd;
> IF NOT FOUND THEN
> RETURN 0;
> END IF;
> RETURN total_sales;
> end;
> ' language 'plpgsql' with (iscachable);
>
>
> JC=# select zoqoo_total_sales(1);
> zoqoo_total_sales
> -------------------
>
> (1 row)
>
> Thanks,
>
> Jc
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Don't 'kill -9' the postmaster
--
----------------------------------------------------------------------------------------
elein(at)varlena(dot)com Database Consulting www.varlena.com
I have always depended on the [QA] of strangers.
From | Date | Subject | |
---|---|---|---|
Next Message | Juraj Fedel | 2003-04-10 01:28:25 | Database Dezign for begginers |
Previous Message | Martijn van Oosterhout | 2003-04-10 00:38:50 | Re: Key features for data warehousing |