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,
fsp.fspro_userid as repid, fsp2.fspro_userid as contactid, int.notes, int.partnerid, int.partnertype, int.cdrDistributorid,
int.reminderDays, int.comment, int.closecomment, int.participantid, ma.MarketingActivityName, par.marketingactivityid, int.interactionMethod,
int.interactionpurposeid, pur.purposeDescription, pur.resourceLinks, pur.resourceLinkNames, ia.name as interactionAttribute,
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,
case when int.partnertype = 'opr'
then oprname.distrib1
else 0 end as primaryDistributorId,
case when ma.MarketingActivityName is not null
then ma.MarketingActivityName
when pur.purposename is not null
then pur.purposename
else 'General Sales Call' end as callType
from tbl_crm_interactions int with (nolock)
left outer join tbl_fspro_members fsp with (nolock)
on fsp.fspro_userid = int.dispositionuserid
left outer join tbl_fspro_members fsp2 with (nolock)
on int.contactid = fsp2.fspro_userid
left outer join tbl_cdr_distributors intdst with (nolock)
on intdst.cdr_recordid = int.cdrDistributorid
left outer join tbl_opr_clientoperators oprname with (nolock)
on oprname.operatorid = int.partnerid
and int.partnertype = 'opr'
left outer join tbl_cdr_distributors cdrname with (nolock)
on cdrname.cdr_recordid = int.partnerid
and int.partnertype in ('cdr','dst')
left outer join tbl_CRM_Participants par with (nolock)
on par.participantid = int.participantid
left outer join tbl_crm_marketingactivities ma with (nolock)
on ma.marketingactivityid = par.marketingactivityid
left outer join tbl_crm_interactionpurposes pur with (nolock)
on coalesce(int.interactionpurposeid,0) = pur.purposeid
and pur.ownerid =
and pur.ownertype =
left outer join tbl_CRM_InteractionAttributes ia with (nolock)
on ia.interactionAttributeId = int.interactionAttributeId
and ia.ownerid = int.ownerid
and ia.ownertype = int.ownertype
where int.ownerid =
and int.ownertype =
and int.interactionid =
select count(*) as itemCount
from tbl_CRM_InteractionAttributes ia with (nolock)
where ownerid =
and ownertype =
select cdr_dstname, cdr_recordid
from tbl_cdr_distributors
where ownerid =
and fsl_tablecode =
and cdr_recordid =