I know how to set a negative value to zero:
select case when (formula) < 0 then 0 else (formula) end as result from tab
But what if "formula" is a very long formula? Then I have to enter it twice. Is there a way to achieve the same result without typing the formula twice?
My actual case looks like this:
select
sum
(
case when
(t1.x + t2.x + t3.x) * t4.p - (t5.x + t6.x + t7.x) * t8.p ) < 0
then 0
else
(t1.x + t2.x + t3.x) * t4.p - (t5.x + t6.x + t7.x) * t8.p )
end
) as result
from
t1
left join t2 on t1.x = t2.x
left join t3 on t1.x = t3.x
... etc ....
t2, t3, t4 etc come from cte statements which deliver values for certain accounts.
