本文介绍了根据ID和之后的组减去值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Oracle.

I'm using Oracle.

具有如下表格:

Year   Type   Value
2011   1       500
2011   2       550
2011   3       600
...
...
2012   1       600
2012   2       750
2012   3       930

我需要从不同类型中减去所有值,并按年份分组.该操作将是:

I need to subtract all the values from different types, grouped by year.The operation would be:

对于2011年-> 1-2-3(500-550-600)

For 2011 -> 1-2-3 (500-550-600)

对于2012年-> 1-2-3(600-750-930)

For 2012 -> 1-2-3 (600-750-930)

对于...

结果应为:

Year   Value
2011   -650
2012   -1080
...    ...

我只是无法使该查询正常工作..

I'm just not managing to get this query working..

推荐答案

使用条件聚合:

select sum(case when type = 1 then value else - value end) as value
from table t
group by year;

这篇关于根据ID和之后的组减去值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 09:34
查看更多