在oracle中键入明智的求和和减法

Type wise summation and subtracting in oracle

我有两个 table 我的商店在 Oracle 上工作。 Image 首先table 描述一下我在商店的交易,有两种类型的交易(MR & SR),MR 表示在商店中添加产品,SR 表示从我的存储中删除产品。我想做的是最终关闭我的存储空间。交易后最终数量每个产品如图所示。我尝试了很多解决方案但无法完成。所以我现在不能显示。请帮我解决这个问题。谢谢

您可以使用下面的案例根据类型减少和增加数量,然后按名称分组,并找到从案例陈述中得出的数量总和以获得您想要的结果。

select row_number() over (order by a.Name) as Sl,a.Name, sum(a.qntity) as qntity
from 
(select t2.Name,case when t1.type='MR' then t2.qntity else -(t2.qntity) end as qntity 
from table1 t1,table2 t2 where t1.oid=t2.table01_oid) a 
group by a.Name;

此查询将提供如下结果:

    SL NAME                 QNTITY

     1 Balls                            0
     2 Books                            6
     3 Pencil                          13