我正在使用
select sum(value1-value2) as Result from tbl
它工作正常,但是当我尝试使用
时select id,name,sum(value1-value2) as Result from tbl
它不起作用。
可以解决什么问题?
答案 0 :(得分:4)
如果您想要每个ID /名称的值,则需要group by
:
select id, name, sum(value1-value2) as Result
from tbl
group by id, name;
否则,您需要将它们放在聚合函数中:
select min(id), min(name), sum(value1-value2) as Result
from tbl;
或者,如果您只是希望每行有所不同,请省略sum()
:
select id, name, (value1-value2) as Result
from tbl;
答案 1 :(得分:1)
使用sum()时,应按
添加分组select id,name,sum(value1-value2) as Result
from tbl
group by id, name