А вы принтом посмотрите какой у вас запрос получается. Я сходу не понял, что вы хотите получить в результате:
Код:
Declare @CRM_FilteredOpportunity VarChar(Max)
Set @CRM_FilteredOpportunity = 'Select * From FilteredOpportunity'
DECLARE @SQL nvarchar(4000)
SET @SQL = 'SELECT name,
ISNULL ((SELECT SUM(new_estimatedvaluethisyear)
FROM ('+@CRM_FilteredOpportunity+') as fa
where (new_mgt = FilteredBusinessUnit.businessunitid)),0) FULLVALUE,
ISNULL ((SELECT SUM(new_estimatedvaluethisyear)
FROM ('+@CRM_FilteredOpportunity+') as fa
where (new_mgt = FilteredBusinessUnit.businessunitid) and (new_Probability = 100000000)),0) F0,
ISNULL ((SELECT SUM(new_estimatedvaluethisyear)
FROM ('+@CRM_FilteredOpportunity+') as fa
where (new_mgt = FilteredBusinessUnit.businessunitid) and (new_Probability = 100000001)),0) F1,
ISNULL ((SELECT SUM(new_estimatedvaluethisyear)
FROM ('+@CRM_FilteredOpportunity+') as fa
where (new_mgt = FilteredBusinessUnit.businessunitid) and (new_Probability = 100000002)),0) F2
from FilteredBusinessUnit
GROUP BY name, businessunitid'
Print @sql
EXEC (@SQL)