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: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] 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 ---------------------------(end of broadcast)--------------------------- TIP 3: Have you checked our extensive FAQ? http://www.postgresql.org/docs/faq