Re: two sums in one query - Mailing list pgsql-sql

From Bruno Wolff III
Subject Re: two sums in one query
Date
Msg-id 20050708140725.GA22725@wolff.to
Whole thread Raw
In response to Re: two sums in one query  (PFC <lists@boutiquenumerique.com>)
Responses Re: two sums in one query
List pgsql-sql
On Fri, Jul 08, 2005 at 15:49:20 +0200, PFC <lists@boutiquenumerique.com> wrote:
> 
> 
> >>   SELECT  SUM( CASE WHEN COALESCE( DEBIT , 0 ) <> 0 THEN
> >>COALESCE( AMOUNT , 0 ) ELSE 0 END ) AS DEBIT_AMT , SUM( CASE WHEN
> >>COALESCE( CREDIT , 0 ) <> 0 THEN   COALESCE( AMOUNT , 0 ) ELSE 0
> >>END ) AS CREDIT_AMT FROM <TABLE NAME>
> 
>     I don't know if it will use indexes (bitmapped OR indexes in 8.1 
>     ?)... if  you have indexes on debit and on credit, you could do
> 
> 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.


pgsql-sql by date:

Previous
From: PFC
Date:
Subject: Re: two sums in one query
Next
From: Steve Wampler
Date:
Subject: Re: Make COUNT(*) Faster?