select interactiondate, fsp.firstname as repfirst, fsp.lastname as replast, fsp2.firstname as contactfirst, fsp2.lastname as contactlast,
int.disposition, intdst.cdr_dstname as distname, cdrname.cdr_dstname as cdrname, oprname.companyname as oprname, int.mileage,
fsp.fspro_userid as repid, fsp2.fspro_userid as contactid, int.notes, int.partnerid, int.partnertype, int.cdrDistributorid,
int.interactionpurposeid, pur.purposeName, int.thankYouSent, int.interactionMethod,
fsp3.fspro_userid as additionalResourceId, fsp3.firstname as additionalResourceFirst, fsp3.lastname as additionalResourceLast,
case when int.partnertype = 'opr'
then oprname.address
else cdrname.cdr_dstaddress1 end as address,
case when int.partnertype = 'opr'
then oprname.city + ' ' + oprname.state + ', ' + oprname.zipcode
else cdrname.cdr_dstcity + ' ' + cdrname.cdr_dststate + ', ' + cdrname.cdr_dstzip end as citystatezip
from tbl_crm_interactions int
left outer join tbl_fspro_members fsp
on fsp.fspro_userid = int.dispositionuserid
left outer join tbl_fspro_members fsp2
on int.contactid = fsp2.fspro_userid
left outer join tbl_cdr_distributors intdst
on intdst.cdr_recordid = int.cdrDistributorid
left outer join tbl_opr_clientoperators oprname
on oprname.operatorid = int.partnerid
and int.partnertype = 'opr'
left outer join tbl_cdr_distributors cdrname
on cdrname.cdr_recordid = int.partnerid
and int.partnertype in ('cdr','dst')
left outer join tbl_crm_interactionpurposes pur
on pur.purposeid = int.interactionpurposeid
left outer join tbl_fspro_members fsp3
on fsp3.fspro_userid = int.additionalResourceUserId
where int.ownerid =
and int.ownertype =
and int.interactionid =
select * from qryPurposes
union
select * from qryPurposesInactive