Received: from localhost.localdomain (postgresql.org [64.49.215.8]) by localhost (Postfix) with ESMTP id EB585476D1C for ; Mon, 8 Jul 2002 14:01:25 -0400 (EDT) Mailbox-Line: From jllachan@nsd.ca Mon Jul 8 14:01:25 2002 Received: from beamish.nsd.ca (unknown [205.150.156.194]) by postgresql.org (Postfix) with ESMTP id 4C013476333 for ; Mon, 8 Jul 2002 11:18:32 -0400 (EDT) Received: (from smap@localhost) by beamish.nsd.ca (8.9.3/8.9.3) id LAA01871; Mon, 8 Jul 2002 11:18:29 -0400 X-Authentication-Warning: beamish.nsd.ca: smap set sender to using -f Received: from reddog.nsd.ca(192.168.101.30) by beamish.nsd.ca via smap (V2.1/2.1+anti-relay+anti-spam) id xma001869; Mon, 8 Jul 02 11:18:22 -0400 Received: from nsd.ca (jllachan-linux.nsd.ca [192.168.101.148]) by reddog.nsd.ca (8.8.7/8.8.7) with ESMTP id LAA06072; Mon, 8 Jul 2002 11:18:21 -0400 Message-ID: <3D29AE37.ADE9D534@nsd.ca> Date: Mon, 08 Jul 2002 11:22:31 -0400 From: Jean-Luc Lachance X-Mailer: Mozilla 4.77 [en] (X11; U; Linux 2.4.9-31 i686) X-Accept-Language: en MIME-Version: 1.0 To: Roy Souther Cc: pgsql-sql@postgresql.org Subject: Re: Bad SUM result References: <200207071425.56235.roy@silicontao.com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Spam-Status: No, hits=-2.7 required=5.0 tests=X_AUTH_WARNING,DOUBLE_CAPSWORD,PGP_SIGNATURE version=2.30 X-Spam-Level: X-Archive-Number: 200207/95 X-Sequence-Number: 8474 That is because your query is generating a cartesian product. Try: SELECT ( SELECT SUM(totalprice) FROM invoices WHERE custnumber = '1' ) - ( SELECT SUM(paymentamount) FROM payments WHERE custnumber = '1' ) Roy Souther wrote: > > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > I have an invoice database that has two tables one for invoices and one for > payments. I want to get the account balance for a client by subtracting the > sum of all payments from the sum off all invoices for that client. > > Here is the SQL that I thought should work. > SELECT SUM(t0.totalprice)-SUM(t1.paymentamount) FROM invoices t0, payments t1 > WHERE t0.custnumber='1' AND t1.custnumber='1' > > It works fine if there is only one invoice and one payment but as soon as > there is more then one of either it screws up. For each match found in > payments the invoice sum is added to the total. So if client 1 purchased a > $100 item then maid a $10 payment the SQL would return the balance of $90 > just fine. When the client makes a second payment of $15 the balance is $75 > but this SQL returns ($100+$100)-($10+$15) = $175. A third payment of $1 > would return ($100+$100+$100)-($10+$15+$1) = $274. > > Could some one explain this to me and recommend an SQL command that would work > please? I could do this using a temp table but that would be very messy as I > would really like it to be a single SQL command. > - -- > Roy Souther > http://www.SiliconTao.com > > Linux: May the source be with you. > > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.0.6 (GNU/Linux) > Comment: For info see http://www.gnupg.org > > iEYEARECAAYFAj0oo9MACgkQCbnxcmEBt43qFQCgtjCs7khKGH+2LYd78O9mA3h4 > vDQAn0GkKkuYl1Kybgm/ITO4LbO1WWLX > =1G4R > -----END PGP SIGNATURE----- > > ---------------------------(end of broadcast)--------------------------- > TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org