You should make this change, because otherwise the DATEDIFF function must be executed against every row in YTD05B and it prevents using any index on Process_Date.where (DATEDIFF(mm, Process_Date, GETDATE()) <= 15)towhere Process_Date >= dateadd(mm,-15,GETDATE())
If YTD05B and YTD06B don't have duplicate rows, change the UNION to UNION ALL to eliminate the grouping operation.The following indicates a problem with the table design if it is necessary to extract part of a column for a join.ON ACT_DATA.dbo.account.account_key = RIGHT(trans.Acnt, 5)
I don't see the point of the following:null as Shares,null as Closing_Price,null as Mktval, cast(Null as varchar(20)) as Cusip_#,
You should change these to only return the columns that are actually needed.SELECT * FROM
CODO ERGO SUM