Simpler Operations

Extracting this code I came across (field names have been redacted):

(CASE WHEN (a.Field1 - b.Field2) > 0 THEN a.Field1 - b.Field2 ELSE 0 END)

Logically not wrong, but the number of mathematical operations can be cut further to increase processing speed.

Zoom in here

(a.Field1 - b.Field2) > 0

There is an arithmetic operation and a comparison operation here. This can be reduced to a single comparison operation with no loss in logic. The arithmetic operation is only executed when the expression is true, otherwise the literal 0 is returned.

(CASE WHEN (a.Field1 > b.Field2) THEN a.Field1 - b.Field2 ELSE 0 END)

Of course I may be wrong as I have not done any benchmarking. (smile)

Leave a Reply

Your email address will not be published. Required fields are marked *