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

From Kenneth Gonsalves
Subject Re: two sums in one query
Date
Msg-id 200507081306.08597.lawgon@thenilgiris.com
Whole thread Raw
In response to two sums in one query  (Kenneth Gonsalves <lawgon@thenilgiris.com>)
Responses Re: two sums in one query
List pgsql-sql
On Friday 08 Jul 2005 12:02 pm, Ramakrishnan Muralidharan wrote:
>    I have assuemed that the row will having eighter Debit account
> or Credit account, the following Query will give sum of debit and
> credit accounts
>
>    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>

actually, all the rows have both 'debit' and 'credit', but based on
the hint you gave i made this query which works:

select
sum(case when debit=account then amount else 0 end) as debtotal,
sum(case when credit=account then amount else 0 end) as credtotal
from voucherrows
where
debit = account
or
credit = account

thanks for taking the trouble - i never believed this was possible,
just asked on the off chance that it *may* be possible
--
regards
kg

http://www.livejournal.com/users/lawgon
tally ho! http://avsap.org.in
ಇಂಡ್ಲಿನಕ್ಸ வாழ்க!


pgsql-sql by date:

Previous
From: Mathew Winters
Date:
Subject: Re: PostgreSQL and Delphi 6
Next
From: "jimmy.olsen"
Date:
Subject: Re: getting back autonumber ... Another MsSQL Comparation Question