The fact is that all the expressions in the list SELECTare evaluated in a way all at once. This is why you need to replicate your code. But you can create one subqueryfor this or ctehow:
with cte as(
select Amount1,
ComponentID,
CASE
WHEN ((select top 1 stuksweergeven from componenten where componentid = componentlink.componentid) = 1) and ((select opbrengstperkilo from componenten where componentid = componentlink.componentid) <> 0)
THEN amount1 * (select opbrengstperkilo from componenten where componentid = componentlink.componentid)
ELSE amount1
END AS Total
from SomeTable)
select Total,
Amount1 * Total *(SELECT dbo.SelectReceptenLinkGewicht(Componentid,0)) AS TotalWeight
from cte
Or:
select Total,
Amount1 * Total *(SELECT dbo.SelectReceptenLinkGewicht(Componentid,0)) AS TotalWeight
from (
select Amount1,
ComponentID,
CASE
WHEN ((select top 1 stuksweergeven from componenten where componentid = componentlink.componentid) = 1) and ((select opbrengstperkilo from componenten where componentid = componentlink.componentid) <> 0)
THEN amount1 * (select opbrengstperkilo from componenten where componentid = componentlink.componentid)
ELSE amount1
END AS Total
from SomeTable) t
source
share