0

I have Oracle SQL query. In results I get something like that:

CODE || VALUE
I || 10
II || 30
III || 50
IV || -20
V || 60
VII || -45
VIII || 0
IX || 100
X || 0

Next I want for CODE = X create formule like:

CASE 1:

SUM OF I,II,III,IV,V,IX

CASE 2:

I+II+III-IV-V+IX

How can I implent these formule in sql query?

2 Answers 2

1

You would use aggregation:

select sum(case when code in ('I', 'II', 'III', 'IV', 'V', 'IX') then value else 0 end) as case1,
       sum(case when code in ('I', 'II', 'III', 'IX') then value
                when code in ('IV', 'V') then -value
                else 0
           end) as case2
from t;
Sign up to request clarification or add additional context in comments.

Comments

0
      select sum(value) from table where code not like 'VI%'   
      Union
      select sum(case when code in ('IV', 'V' ) then -value
      else value end case) from table where code not like 
        'VI%'   

Just a different way of doing the same as above via union

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.