Contact1 tables pertinent fields are companyname, companycontact, contactphone, accountnum. This table links to a Contacthistory table by way of accountnum field. Contacthistory tables pertinent fields are accountnum, SalesRep (known as TsgUser), ContactActivitydate, Contactactivitytype (Call, Email, PhoneCall etc).
I need to know what accounts that no contact (from that specific account)has been called, visited, or emailed by a Salesrep. The query below gives me all old account contact records but does not exclude accounts that have 1 account contact visited, called, or emailed within the year and another contact (the same account) with activity older than 1 year.
This almost-right query is being written in Crystal sql designer:
SELECT DISTINCT
ContactHistory.`ACCNTNUM`,
ContactHistory.`LASTDATE`,
ContactHistory.`RECTYPE`,
ContactHistory.`TSGUSER`,
Contact1.`COMPANYNAME`,
Contact1.`LASTNAME`,
Contact1.`WORKPHONE`,
MONTH (ContactHistory.`LASTDATE`),
YEAR (ContactHistory.`LASTDATE`)
FROM
`ContactHistory` ContactHistory INNER JOIN `ContactHistory` Contact1 ON
ContactHistory.`ACCNTNUM` = Contact1.`ACCNTNUM`
WHERE
ContactHistory.`RecType` IN ( 'Call','Appt','Email' ) AND
ContactHistory.`TsgUSER` IN ( 'LARRY','WILLY','JOE','BOB','MARY','JOHN','HARRY','BRIAN' ) AND
ContactHistory.`ACCNTNUM` IS NOT NULL
GROUP BY ContactHistory.`Accntnum`, ContactHistory.`LastDate`, ContactHistory.`RecType`, ContactHistory.`TsgUser`, ContactHistory.`CompanyName`, ContactHistory.`LastName`, ContactHistory.`WorkPhone`, MONTH(ContactHistory.`Lastdate`), YEAR(ContactHistory.`LastDate`)
HAVING MAX(ContactHistory.`LastDate`) < {ts '2001-01-01 00:00:00.000'}