From: Ramakrishnan Muralidharan <ramakrishnanm@pervasive-postgres.com>
To: Kenneth Gonsalves <lawgon@thenilgiris.com>
To: pgsql-sql@postgresql.org
Subject: Re: two sums in one query
Date: Fri, 8 Jul 2005 12:02:28 +0530
Message-ID: <02767D4600E59A4487233B23AEF5C5992A4093@blrmail1.aus.pervasive.com> (raw)
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@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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-sql@postgresql.org
Cc: ramakrishnanm@pervasive-postgres.com, lawgon@thenilgiris.com
Subject: Re: two sums in one query
In-Reply-To: <02767D4600E59A4487233B23AEF5C5992A4093@blrmail1.aus.pervasive.com>
References: <02767D4600E59A4487233B23AEF5C5992A4093@blrmail1.aus.pervasive.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox