Re: two sums in one query

From: "Ramakrishnan Muralidharan" <ramakrishnanm(at)pervasive-postgres(dot)com>
To: "Kenneth Gonsalves" <lawgon(at)thenilgiris(dot)com>, <pgsql-sql(at)postgresql(dot)org>
Subject: Re: two sums in one query
Date: 2005-07-08 06:32:28
Message-ID: 02767D4600E59A4487233B23AEF5C5992A4093@blrmail1.aus.pervasive.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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>

Regards,
R.Muralidharan

-----Original Message-----
From: pgsql-sql-owner(at)postgresql(dot)org
[mailto:pgsql-sql-owner(at)postgresql(dot)org]On Behalf Of Kenneth Gonsalves
Sent: Friday, July 08, 2005 11:18 AM
To: pgsql-sql(at)postgresql(dot)org
Subject: [SQL] two sums in one query

hi
i have a table like this:

serialno varchar,
debit integer,
credit integer,
amount numeric

the columns 'debit' and 'credit' refer to the acount number. I want to
query the table to select all the rows where either debit = account
or credit=account and to get two sums - one of the amounts where the
account is on the debit side and the other on the credit side. Can i
do this in one query? if so, how?
--
regards
kg

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

---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Kenneth Gonsalves 2005-07-08 07:36:08 Re: two sums in one query
Previous Message Mathew Winters 2005-07-08 06:22:47 Re: PostgreSQL and Delphi 6