select int.interactionid, int.InteractionDate, int.createDate, sku.skuid, sku.sku, sku.skuDesc, sku.packSizeDesc, mfr.mfr_id, mfr.mfr_name, sku.discontinued, case when int.partnertype = 'opr' then opr.companyname else cdr.cdr_dstname end as companyname, case when int.partnertype = 'opr' then opr.companyname + cast(int.#attributes.dateField# as varchar) else cdr.cdr_dstname + cast(int.#attributes.dateField# as varchar) end as companynamegroup select distinct int.InteractionID, int.#attributes.dateField# from tbl_CRM_Interactions int inner join tbl_CRM_InteractionSaleDetails isd on isd.InteractionID = int.InteractionID and isd.SaleSubjectType = 'sku' inner join tbl_PRD_SKUs sku on sku.skuid = isd.SaleSubjectId and sku.mfrid = and sku.discontinued = 'n' inner join tblmanufacturers mfr on mfr.mfr_id = sku.mfrid left outer join tbl_OPR_ClientOperators opr on opr.OperatorID = int.PartnerId and int.PartnerType = 'opr' left outer join tbl_CDR_Distributors cdr on cdr.cdr_recordid = int.PartnerId and int.PartnerType in ('dst','cdr') where int.ownerid = and int.ownertype = and int.DispositionUserId = and int.InteractionID = and ( sku.sku like or sku.skuDesc like or opr.companyname like or cdr.cdr_dstname like ) and datediff(DAY,int.#attributes.dateField#,GETDATE()) <= and int.interactiondate is not null and int.interactionid not in () and int.disposition <> '?' order by int.#attributes.dateField# desc, companyname, int.interactionid, mfr.mfr_name, sku.sku order by int.#attributes.dateField# desc, int.InteractionID desc