Q:

Issue with using SUM function in SQL

I am using 
select sum(value1-value2) as Result from tbl

it is working fine but when I try to use
select id,name,sum(value1-value2) as Result from tbl

it does not work.
What can be the solution?

A:

You need a group by if you want the value for each id/name:
select id, name, sum(value1-value2) as Result
from tbl
group by id, name;

Otherwise, you need to put them in aggregate functions:
select min(id), min(name), sum(value1-value2) as Result
from tbl;

Or, if you just want the difference on each row, then omit the sum():
select id, name, (value1-value2) as Result
from tbl;

