SELECT [FDMSAccountNo],[Intchg_Assess],
ISNULL(MSC,0) - ISNULL([Intchg_Assess],0) as DI
from
(SELECT
d.[FDMSAccountNo],
d.[Intchg_Assess],
SUM(CASE WHEN merch_purch_fees < 0 THEN [Merch_Purch_Fees] WHEN merch_purch_fees > = 0 THEN [Merch_Purch_Fees] + [Per_Tran_Fees] END) AS 'MSC'
FROM [FDMS].[dbo].[Fact_Omnipay_Profitability] d
where (convert(datetime,Period,103) BETWEEN @Rolling12tempFROM and @Rolling12tempTO) and d.FDMSAccountNo in (select FDMSAccountNo from #Accounts)
GROUP BY d.FDMSAccountNo,d.[Intchg_Assess]
)G