PDA

View Full Version : Summation



zawedx
03-26-2009, 12:08 PM
if i have a table like:
id | btype | bnum
1 | 0 | 10
1 | 0 | 5
1 | 10 | 4
1 | 10 | 3

is it possible to use a single SQL command to query the different of "bnum" base on "btype" of id "1". it should be (10+5)-(4+3) =8.

if not, what is the smartest way to do this.

thanks a lot for your kind help.

Fumigator
03-26-2009, 05:06 PM
Yeah... just group by the columns that you want to sum on. In your case, group by id, btype.



SELECT id, btype, SUM(bnum)
FROM table1
GROUP BY id, btype