Re: Please some help on a join question with sum

From: Oliver Elphick <olly(at)lfix(dot)co(dot)uk>
To: Feite Brekeveld <f(dot)brekeveld(at)osiris-it(dot)nl>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Please some help on a join question with sum
Date: 2003-04-19 18:59:51
Message-ID: 1050778790.4168.8.camel@linda.lfix.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Sat, 2003-04-19 at 19:52, Oliver Elphick wrote:

> SELECT aa.id,
> aa.seqno,
> aa.price + (
> SELECT SUM(bb.price)
> FROM bb
> WHERE bb.id = aa.id AND bb.seqno = aa.seqno
> ) AS price
> FROM aa;

If there aren't any matching records in bb, this won't work because the
sub-select will produce null, so it should actually be:

SELECT aa.id,
aa.seqno,
aa.price + COALESCE(
(SELECT SUM(bb.price)
FROM bb
WHERE bb.id = aa.id AND bb.seqno = aa.seqno),
0) AS price
FROM aa;

--
Oliver Elphick Oliver(dot)Elphick(at)lfix(dot)co(dot)uk
Isle of Wight, UK http://www.lfix.co.uk/oliver
GPG: 1024D/3E1D0C1C: CA12 09E0 E8D5 8870 5839 932A 614D 4C34 3E1D 0C1C
========================================
"For I am persuaded, that neither death, nor life, nor
angels, nor principalities, nor powers, nor things
present, nor things to come, nor height, nor depth,
nor any other creature, shall be able to separate us
from the love of God, which is in Christ Jesus our
Lord." Romans 8:38,39

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Josh Berkus 2003-04-19 19:03:18 Re: [SQL] Yet Another (Simple) Case of Index not used
Previous Message Oliver Elphick 2003-04-19 18:52:35 Re: Please some help on a join question with sum