带计算列的SQL Sum

时间:2019-12-18 15:22:50

标签: sql sum

您好,我有一个SQL查询选择第一列中的SUM值,我希望第二列为(first_column / another_field)

select sum(a.amount), (sum(a.amount) / d.total_loading_weight * 1000) as MarginPerTon
from tra_affair a join tra_Delivery d on a.delivery=d.delivery_id
where a.delivery='394179' and a.is_margin='1'

我收到“不是单组分组功能”错误

1 个答案:

答案 0 :(得分:1)

问题是:d.total_loading_weight。我不确定您想要什么,但是类似:

select sum(a.amount),
       (sum(a.amount) / sum(d.total_loading_weight * 1000)) as MarginPerTon
from tra_affair a join
     tra_Delivery d
     on a.delivery = d.delivery_id
where a.delivery = '394179' and a.is_margin = '1';

如果值始终相同,也可以使用min()max()代替sum()