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

From Ramakrishnan Muralidharan
Subject Re: two sums in one query
Date
Msg-id 02767D4600E59A4487233B23AEF5C5992A4093@blrmail1.aus.pervasive.com
Whole thread Raw
In response to two sums in one query  (Kenneth Gonsalves <lawgon@thenilgiris.com>)
List pgsql-sql
I have assuemed that the row will having eighter Debit account or Credit account, the following Query will give sum
ofdebit 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@postgresql.org
[mailto:pgsql-sql-owner@postgresql.org]On Behalf Of Kenneth Gonsalves
Sent: Friday, July 08, 2005 11:18 AM
To: pgsql-sql@postgresql.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

pgsql-sql by date:

Previous
From: Richard Hayward
Date:
Subject: function parameters : bug?
Next
From: "Riya Verghese"
Date:
Subject: ORDER records based on parameters in IN clause