askolotl
askolotl

Reputation: 1006

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.

Upvotes: 3

Views: 131

Answers (3)

Zhorov
Zhorov

Reputation: 29983

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)

Upvotes: 5

Dale K
Dale K

Reputation: 27249

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.

Upvotes: 4

Han
Han

Reputation: 3072

Use CTE.

DECLARE @Table1 TABLE
(
    ID INT,
    A INT,
    B INT
);

DECLARE @Table2 TABLE
(
    ID INT,
    C INT,
    D INT
);

INSERT @Table1 VALUES
    ( 1,  1,  2),
    ( 2,  3, -4),
    ( 3, -5, -6);

INSERT @Table2 VALUES
    ( 1, -7,  8),
    ( 2,  9, 10),
    ( 3, 11, 12);
    
WITH query (Formula)
AS
(
    SELECT (A + B) * (C - D) AS Formula
        FROM @Table1 T1 INNER JOIN @Table2 T2 ON T1.ID = T2.ID
)
SELECT Formula, CASE WHEN Formula < 0 THEN 0 ELSE Formula END AS Result
    FROM query

Result:

enter image description here

Upvotes: 3

Related Questions