I have a oracle query and part of it is calculating some value using DECODE. For example:
SELECT ...,
(SELECT DECODE((SELECT 23 FROM DUAL),
0, null,
(SELECT 23 FROM DUAL))
FROM DUAL)
FROM ...
Here the value “23” gets calculated at runtime, and it’s quite complicated joins – multiple tables, uses PARTITION BY etc. So I want to avoid executing the same subquery if the value is not “0”. Is there any way to write something like this
SELECT ...,
(SELECT DECODE ((SELECT 23 FROM DUAL) as test,
0, null,
test)
FROM DUAL)
FROM ...
Will this work for you?
I’ve just moved the “23” to an inline table with a descriptive alias.
A CASE statement might also add clarity, as in: