Using the variable -
DECLARE @SumA INT = SELECT SUM(Col1) FROM TableA WHERE ... etc DECLARE @SumB INT = SELECT SUM(Col2) FROM TableB WHERE ... etc SELECT x.id , x.name , x.type ,( CASE x.type WHEN 1 THEN @SumA WHEN 2 THEN @SumB END ) AS Total FROM TableX as x
Select the data type for the Sum variable accordingly (if Decimal).
source share