I have customers table and a another one for the sales activities associated to them (phone calls, emails ecc).
What I need is to know is which of those customers haven’t been contacted (without activities) over the last 30 days for example.
Here is what I’m trying to do:
SELECT crm_customers.id, crm_ customers.companyname
FROM crm_ customers
LEFT JOIN crm_activities on crm_customers.id = crm_ activities. Customerid
WHERE crm_ activities.createddate < (getDate() – 30)
The problem is that it returns the customers with activities older than 30 days even if they have more recent activities. I need to get only the customers without any sales activities in the last 30 days
Thanks for your help
This is a case of condition in the join is different than condition in the where
For speed I would suspect the join over the subquery. It gives the query optimizer more option to optimize. Does not required a distinct. Assume crm_activities.Customerid is indexed this is joining on the index. With the subquery it would use the index to create the list but output of the subquery is not indexed.
Will depend on you datar. I had a big table to test on.
Query plans are different.
On a loop join it was dead heat
On a hash join twice as fast
On a merge join ten times as fast
If none are excluded then it will be a dead heat.
Where it will show up is if many are excluded (have been contacted in the last 30).