Try this structure. SELECT [name] ,owneridname AS AccountManager ,new_telemarketingownername AS TelemarketingOwner ,accountid ,new_autonumbertext ,oa1.LastCallDate ,oa1.subject ,oa1.owneridname ,oa2.owneridname ,oa2.scheduledendFROM FilteredAccount AS faOUTER APPLY ( select top 1 fpc1.actualend AS LastCallDate ,fpc1.subject ,fpc1.owneridname FROM FilteredPhoneCall AS fpc1 INNER JOIN FilteredActivityParty AS fap1 ON fap1.activityid = fpc1.activityid INNER JOIN FilteredContact AS fc1 ON fc1.contactid = fap1.partyid where fc1.accountid = fa.accountid and fap1.participationtypemaskname = N'To Recipient' AND fpc1.statecode = '1' order by fpc1.actualend DESC ) oa1OUTER APPLY ( select top 1 fpc1.owneridname ,fpc1.scheduledend FROM FilteredPhoneCall AS fpc1 INNER JOIN FilteredActivityParty AS fap1 ON fap1.activityid = fpc1.activityid INNER JOIN FilteredContact AS fc1 ON fc1.contactid = fap1.partyid where fc1.accountid = fa.accountid and fap1.participationtypemaskname = N'To Recipient' AND fpc1.statecode = '0' order by fpc1.scheduledend ) oa2WHERE fa.statuscode = '1'
Be One with the OptimizerTG