I'm using Oracle.
Having a table as:
Year Type Value
2011 1 500
2011 2 550
2011 3 600
...
...
2012 1 600
2012 2 750
2012 3 930
I needed to subtract all the values from different types, grouped by year. The operation would be:
For 2011 -> 1-2-3 (500-550-600)
For 2012 -> 1-2-3 (600-750-930)
For ...
The result shoud be:
Year Value
2011 -650
2012 -1080
... ...
I couldn't do it but here on stack overflow this query was suggested, and it worked:
select sum(case when type = 1 then value else - value end) as value
from table t
group by year;
But now, i have another situation. I need to do the same thing but not 1-2-3-4-5-... But 1+2-3-4-5-6....
To to this i tried this both queries, with no sucess:
select sum(case when type = 1 then value when type = 2 then value else - value end) as value
from table t
group by year;
This resulted in the second value has its value doubled.
select sum(case when type = 1 then value else - value end)+case when type = 2 then value as value
from table t
group by year;
This resulted in a correct type 2 value, but,as this type 2 only accurs in some years, the other years show up as null. So, the final calculation is correct for type 2 (the years it is in) but for every other year, for each type 2 does not exist, it returns null.
I'm just not managing to get this query working.. Any idea would be greatly appreciated! Thanks