SQL中的减法查询

时间:2017-03-14 11:50:27

标签: sql oracle oracle11g

我有以下数据

req.end

我需要编写一个查询,它将把summa_som = 7594 - 7392 = 202作为INCOME summa_som我使用函数

进行更新
select * from t_operation

date_operation ntype_operation  summa    summa_som course_valname datecourse
-------------- ---------------  -----    --------- -------------- ----------
3/15/20017        BUY            100       7392       EUR          3/15/20017  
3/15/20017        SELL           100       7594       EUR          3/15/20017 

功能

update t_operation
set summa_som = get_rate(date_operation,course_valname,upper(substr(ntype_operation,1,1))) * trunc(summa)
where summa is null

2 个答案:

答案 0 :(得分:1)

我认为你只想要条件聚合:

select amount_conv,
       sum(case when type_operation = 'SELL' then amount
                when type_operation = 'BUY' then - amount
                else 0
           end) as income,
       course
from t
group by amount_conv, course;

我不确定你为什么要在第二栏中SELL。另一方面,course似乎是放入结果集的有用列。

答案 1 :(得分:1)

假设您有一个列定义值的配对方式:

select SomeOtherField, -- This is the column that defines which values become pairs
       sum(case
             when type_operation = 'SELL' then ammount_conv 
             else ammount_conv * -1 
           end) as Profit
from MyTable
group by SomeOtherField