I am making a storedprocedure which has a SELECT clause selecting multiple colums from some joined tables. For one of these colums the value is depending on a condition with CASE.
SELECT DISTINCT
table1.col1 as var1,
table2.col2 as var2,
CASE WHEN (FLOOR(table3.col3/0.2)*0.2) > 20 THEN 20
WHEN (FLOOR(table3.col3/0.2)*0.2) <= 20 THEN FLOOR(table3.col3/0.2)*0.2
ELSE table3.col4 -- selecting another value is the
-- value in table3.col3 is null
END as var3
FROM ...
WHERE ...
As you can see in the select I’m doing three times the FLOOR(table3.col3/0.2)*0.2 calculation. Is it possible to store this calculated value in a variable WITHIN THE SELECT and then use this variable in the conditions?
Thanks in advance,
Jeroen
No, you can reduce it to 2 calls like below. But whatever else you will do will be more costly then actually doing it like this, as what you do there is very fast. You can think on something to improve, when you actually have some heavy operations. This is just nothing. You might also make a get_min function but again I do not think it will be worth it.
With 2 calls:
With min function:
As a side note, the min function I am referring to has to be built (user defined function).