Thread: Sum sets of records
Hi list,<br /><br />I would like a little help from you to get the sum value of various subsets of my table.<br /><br />Mytable is like this<br /><br />id | type | value<br />1 C 10.00<br />2 C 15.00<br />3 W 5.00<br/> 4 W 3.00<br /><br />Where C is credit and w is witdraw.<br /><br />How do I sum all records with C andsubtract with all records with type W ?<br /><br />Is it possible ?<br /><span style="font-weight: bold;">Ezequias</span><brclear="all" /><br />
select sum(case when id = 'C' then value else -value end) as total
from mytable
>>> "Ezequias Rodrigues da Rocha" <ezequias.rocha@gmail.com> 2007-02-12 16:41 >>>
Hi list,
I would like a little help from you to get the sum value of various subsets of my table.
My table is like this
id | type | value
1 C 10.00
2 C 15.00
3 W 5.00
4 W 3.00
Where C is credit and w is witdraw.
How do I sum all records with C and subtract with all records with type W ?
Is it possible ?
Ezequias
Hi, "Group by" is what you're looking for.. select type,sum(value) from MyTable group by type Or for the total only Select sum(case when type='C' then value when type='W' then -value else 0 end) from MyTable hth Sylvain Barrette Architecte et analyste principal, RE/MAX Québec inc. Hi list, I would like a little help from you to get the sum value of various subsets of my table. My table is like this id | type | value 1 C 10.00 2 C 15.00 3 W 5.00 4 W 3.00 Where C is credit and w is witdraw. How do I sum all records with C and subtract with all records with type W ? Is it possible ? Ezequias -- No virus found in this outgoing message. Checked by AVG Free Edition. Version: 7.1.411 / Virus Database: 268.17.36/681 - Release Date: 2007-02-11