I don't think your SQL query does what you want it to - you seem to be comparing an email address against the string '2', rather than counting anything
You can't do this in one QueryExpression. Probably your best option if using CRM queries is:
- Use a FetchExpression that returns accounts with a count of the number of contacts they have
- Interate through these accounts to find those that have 2 or more contacts (you can't filter these in the query, as Fetch has no equivalent of a Having clause)
- Query the contacts that belong to these accounts. If the number of matching accounts is less than ~1000 you can do this in one query with a large IN expression, but if there are more than these you could hit a SQL limit in the number of parameters in one
query, so you'd need separate queries
Alternatively, if you have CRM OnPremise, then you could use the System.Data classes to query the CRM filtered views
Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk