Is there an advantage of using Cross apply over case statements? For example as below I have a number of columns I need to do calculations on, is it inefficient to use cross apply to achieve this or is there no performance difference?
SELECT * from myTable
cross apply
(
select test1 =
CASE
WHEN Price <> '' AND Cost <> 0 and
Cast(Cost As float) <> -1 THEN Cost + Price
WHEN Price <> '' AND Cost = 0 THEN bestPrice
WHEN Price <> '' AND Cost = -1 THEN CAST(Cost AS varchar(20))
END
) as test1
cross apply
(
select Handling = round(((Price * 1.2) + 2.5) / 0.85, 2)
) as Handling
WHERE (accuracy > 0 and Total <> -1)
There is no performance difference. You can verify this by looking at the execution plans of both variants.
A cross-apply in this pattern is just a “Compute Scalar”. The query optimizer is smart about this.