two sums in one query
two sums in one query
От:
Kenneth Gonsalves <lawgon@thenilgiris.com>
Дата:
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 ಇಂಡ್ಲಿನಕ್ಸ வாழ்க!
Re: two sums in one query
От:
Kenneth Gonsalves <lawgon@thenilgiris.com>
Дата:
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
Re: two sums in one query
От:
PFC <lists@boutiquenumerique.com>
Дата:
>> 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
Re: two sums in one query
От:
Bruno Wolff III <bruno@wolff.to>
Дата:
On Fri, Jul 08, 2005 at 15:49:20 +0200, PFC wrote: > > > >> 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
Re: two sums in one query
От:
PFC <lists@boutiquenumerique.com>
Дата:
>> SELECT (SELECT sum(amount) FROM table WHERE debit=X) AS debit, (SELECT >> sum(amount) FROM table WHERE credit=x) AS credit; > > If most of the records are credits or debits you don't want to do this. > A single sequential scan through the table will be the best plan. I thought that debit = source account # and credit = dest account #, and there are a lot of different account...
Re: two sums in one query
От:
Bruno Wolff III <bruno@wolff.to>
Дата:
On Fri, Jul 08, 2005 at 16:49:44 +0200, PFC wrote: > > >>SELECT (SELECT sum(amount) FROM table WHERE debit=X) AS debit, (SELECT > >>sum(amount) FROM table WHERE credit=x) AS credit; > > > >If most of the records are credits or debits you don't want to do this. > >A single sequential scan through the table will be the best plan. > > I thought that debit = source account # and credit = dest account #, > and there are a lot of different account... You aren't likely to see a significant speed up in that case either.
Re: two sums in one query
От:
"Ramakrishnan Muralidharan" <ramakrishnanm@pervasive-postgres.com>
Дата:
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