0

我在用

select sum(value1-value2) as Result from tbl

它工作正常,但是当我尝试使用时

select id,name,sum(value1-value2) as Result from tbl

这没用。

有什么解决办法?

4

2 回答 2

4

group by如果您想要每个 id/name 的值,则需要 a :

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;
于 2013-07-29T02:20:57.837 回答
1

当你使用 sum() 时,你应该添加 group by

select id,name,sum(value1-value2) as Result
from tbl
group by id, name
于 2013-07-29T02:24:32.657 回答