0

I have this Oracle SQL request:

SELECT col1,
       col2,
       DECODE(
                 SUM(CASE WHEN col3='A' AND col4='+' THEN col5 ELSE 0 END),
                 NULL,
                 0,
                 SUM(CASE WHEN col3='A' AND col4='+' THEN col5 ELSE 0 END)
             )
FROM mytable
group by col1, col2;

I am asking if there is a way to declare a kind of variable and have something like this:

SELECT col1,
       col2,
       DECODE(
                 myVariable,
                 NULL,
                 0,
                 myVariable
             )
FROM mytable
group by col1, col2;
1
  • 2
    How are you intending to use this query? Variables are not a SQL thing but belong to the realm of the software which runs the query. So the solution will depend on whether your want to run the query interactively in some client such as SQL*Plus, as part of a parameterised stored procedure or as a JDBC PreparedStatement (to name just three possibilities). Commented Jan 3, 2019 at 14:48

4 Answers 4

2

no, but you could do a subquery:

SELECT col1,
       col2,
       DECODE(
                 SUM(myColumn),
                 NULL,
                 0,
                 SUM(myColumn)
             )
FROM (
    SELECT 
        col1,
        col2,
        CASE WHEN col3='A' AND col4='+' THEN col5 ELSE 0 END myColumn   
        FROM mytable
     ) a
group by col1, col2;
Sign up to request clarification or add additional context in comments.

Comments

1

You can simlpy use coalesce() (or nvl()) instead of decode().

SELECT col1,
       col2,
       coalesce(sum(CASE
                      WHEN col3 = 'A'
                           AND col4 = '+' THEN
                        col5
                      ELSE
                        0
                    END),
                0)
       FROM mytable
       GROUP BY col1,
                col2;

Comments

1

You can use coalesce(). I think this is sufficient:

select col1, col2,
       coalesce(sum(case when col3 = 'A' and col4 = '+' then col5 end), 0)
from mytable
group by col1, col2;

In actual fact, this expression:

sum(case when col3 = 'A' and col4 = '+' then col5 else 0 end)

Cannot return NULL in a query with a group by -- every group has at least one row and the else guarantees a 0 returns rather than NULL.

So, this should also do what you want:

select col1, col2,
       sum(case when col3 = 'A' and col4 = '+' then col5 end)
from mytable
group by col1, col2;

Comments

0

Yes you can use substitution variables:

SELECT col1,
       col2,
       DECODE(
                 &&myVariable,
                 NULL,
                 0,
                 &&myVariable
             )
FROM mytable
group by col1, col2;

More info here Oracle SQL*Plus Substitution Variables

1 Comment

I believe the intent is to treat SUM(CASE WHEN col3='A' AND col4='+' THEN col5 ELSE 0 END) as a "variable" so it does not need to be repeated, not to pass in arbitrary amounts as variables,

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.