Thread: Please some help on a join question with sum aggregate

Please some help on a join question with sum aggregate

From
Feite Brekeveld
Date:
Hi,

I havbe the following SQL related question.

Table AA:

id, seqno, price

A10    1    1000.0

A10    2    2000.0



Table BB:


id    seqno        subseq    price

A10       1        1        10

A10       1        2        20

A10       1        3        30

A10        2        1        25


I would like to have a query that joins table AA and BB to the result:

id    seqno    price
A10    1        1060
A10    2        2025

Thanks,

Feite Brekeveld


Re: Please some help on a join question with sum

From
Oliver Elphick
Date:
On Sat, 2003-04-19 at 19:29, Feite Brekeveld wrote:
> Hi,
>
> I havbe the following SQL related question.
>
> Table AA:
>
> id, seqno, price
>
> A10    1    1000.0
>
> A10    2    2000.0
>
>
>
> Table BB:
>
>
> id    seqno        subseq    price
>
> A10       1        1        10
>
> A10       1        2        20
>
> A10       1        3        30
>
> A10        2        1        25
>
>
> I would like to have a query that joins table AA and BB to the result:
>
> id    seqno    price
> A10    1        1060
> A10    2        2025


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;

--
Oliver Elphick                                Oliver.Elphick@lfix.co.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


Re: Please some help on a join question with sum

From
Oliver Elphick
Date:
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.Elphick@lfix.co.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


Re: Please some help on a join question with sum aggregate

From
elein
Date:
The other alternative is to use:

select AA.id, AA.seq, AA.price + coalesce(sum(BB.price),0)
from AA left outer join BB using (id, seq)
group by AA.id, AA.seq, AA.price;

The sum and group by seems more natural to me.  The coalesce
takes care of the case where there is nothing in table BB,
but there is something (and you want to see it) in AA.

elein

On Saturday 19 April 2003 11:29, Feite Brekeveld wrote:
> Hi,
>
> I havbe the following SQL related question.
>
> Table AA:
>
> id, seqno, price
>
> A10    1    1000.0
>
> A10    2    2000.0
>
>
>
> Table BB:
>
>
> id    seqno        subseq    price
>
> A10       1        1        10
>
> A10       1        2        20
>
> A10       1        3        30
>
> A10        2        1        25
>
>
> I would like to have a query that joins table AA and BB to the result:
>
> id    seqno    price
> A10    1        1060
> A10    2        2025
>
> Thanks,
>
> Feite Brekeveld
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: Have you checked our extensive FAQ?
>
> http://www.postgresql.org/docs/faqs/FAQ.html

--
----------------------------------------------------------------------------------------
elein@varlena.com     Database Consulting     www.varlena.com
              I have always depended on the [QA] of strangers.