X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 814DB5285C for ; Fri, 8 Jul 2005 12:55:33 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 21403-08 for ; Fri, 8 Jul 2005 15:55:31 +0000 (GMT) Received: from wolff.to (wolff.to [66.93.197.194]) by svr1.postgresql.org (Postfix) with SMTP id 6DE0B5281A for ; Fri, 8 Jul 2005 12:55:29 -0300 (ADT) Received: (qmail 29565 invoked by uid 500); 8 Jul 2005 15:55:50 -0000 Date: Fri, 8 Jul 2005 10:55:50 -0500 From: Bruno Wolff III To: PFC Cc: Kenneth Gonsalves , Ramakrishnan Muralidharan , pgsql-sql@postgresql.org Subject: Re: two sums in one query Message-ID: <20050708155550.GA28683@wolff.to> Mail-Followup-To: Bruno Wolff III , PFC , Kenneth Gonsalves , Ramakrishnan Muralidharan , pgsql-sql@postgresql.org References: <02767D4600E59A4487233B23AEF5C5992A4093@blrmail1.aus.pervasive.com> <200507081306.08597.lawgon@thenilgiris.com> <20050708140725.GA22725@wolff.to> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.6i X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.007 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200507/79 X-Sequence-Number: 22057 On Fri, Jul 08, 2005 at 16:49:44 +0200, PFC wrote: > > >>SELECT (SELECT sum(amount) FROM table WHERE debit=X) AS debit, (SELECT > >>sum(amount) FROM table WHERE credit=x) AS credit; > > > >If most of the records are credits or debits you don't want to do this. > >A single sequential scan through the table will be the best plan. > > I thought that debit = source account # and credit = dest account #, > and there are a lot of different account... You aren't likely to see a significant speed up in that case either.