Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a negative result to zero without typing the formula twice

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.

like image 397
askolotl Avatar asked Jan 26 '26 01:01

askolotl


2 Answers

You may try the following approaches:

  • VALUES table value constructor and MAX aggregate function.
  • APPLY operator and MAX aggregate function.

T-SQL:

SELECT (SELECT MAX(result) FROM (VALUES (0), (tab.x)) v(result)) AS result
FROM (VALUES
   (2 - 3),
   (4 + 5)
) tab (x)

SELECT apl.result
FROM (VALUES
   (2 - 3),
   (4 + 5)
) tab (x)
CROSS APPLY (SELECT MAX(x) FROM (VALUES (0), (tab.x)) v (x)) apl (result)

The following example, based on the code from the question, is a possible solution:

SELECT x INTO t1 FROM (VALUES (-1), (1)) v (x)
SELECT x INTO t2 FROM (VALUES (-1), (1)) v (x)
SELECT x INTO t3 FROM (VALUES (-1), (1)) v (x)

SELECT SUM(a.result)
FROM t1 
LEFT JOIN t2 ON t1.x = t2.x 
LEFT JOIN t3 ON t1.x = t3.x
CROSS APPLY (
   SELECT MAX(x) FROM (VALUES (0), (t1.x + t2.x + t3.x)) v (x)
) a (result)
like image 200
Zhorov Avatar answered Jan 28 '26 14:01

Zhorov


This can be accomplished using a simple sub-query e.g.

select
    sum
    ( 
        case when ForumulaResult < 0
        then 0 
        else ForumulaResult
        end
    ) as result
from (
    select
        (t1.x + t2.x + t3.x) * t4.p - (t5.x + t6.x + t7.x) * t8.p) as ForumulaResult
    from t1 
    left join t2 on t1.x = t2.x 
    left join t3 on t1.x = t3.x 
    -- ... etc ....
) x;

Or using cross apply

select
    sum
    ( 
        case when ForumulaResult < 0
        then 0 
        else ForumulaResult
        end
    ) as result
from t1 
left join t2 on t1.x = t2.x 
left join t3 on t1.x = t3.x 
cross apply (select (t1.x + t2.x + t3.x) * t4.p - (t5.x + t6.x + t7.x) * t8.p ) as ForumulaResult) x

Or using a CTE as already shown... although you indicate you already have a CTE generating the starting data... so it might be harder to combine.

On the face of it, with simple data as provided by @Han, they all produce the same execution plan, so the choice of which to use it really up to which is clearer for you to use.

like image 24
Dale K Avatar answered Jan 28 '26 15:01

Dale K



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!