Jean-Christian Imbeault <jc@mega-bucks.co.jp> writes:
> 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.
SUM() over no rows returns NULL, not zero, per the SQL spec.
(Yes, it's a stupid spec.)
Your IF NOT FOUND test can never succeed, because the select will always
return exactly one row no matter what. Try testing total_sales for NULL
instead.
regards, tom lane