+ Start a Discussion
Chris Sanchez 5Chris Sanchez 5 

Account Opportunity SOQL

Hello - Please advise how to write the following query.

Select all accounts that have at least one opportunity with the text "2016" in the name and also does not have any opportunities with the text "2017" and "2018" in the name. 

I tried my hand with reports and custom report types but as easy as this sounds no such luck.  I appreciate your help.

 
Sylvio AvillaSylvio Avilla
Try this
 
SELECT OpportunityId FROM Opportunity
WHERE Name LIKE '%2016%'

 
Chris Sanchez 5Chris Sanchez 5
Hello – Thanks for the reply, your response only gives me part of the query. I need to get a list of all accounts that have at least one opportunity with the text “2016” in the name and also don’t have any other opportunities with the text “2017” and “2018” in the name. So a generalization of the query I need is something like… Select All Accounts Where at least one opportunityName contains “2016” AND all opportunityNames don’t include “2017” and “2018” Thanks, Chris
Sylvio AvillaSylvio Avilla
Ok, something like this?
 
SELECT AccountId FROM Opportunity WHERE Name LIKE '%2016%'

 
Sam SwannSam Swann
Hope this helps:
 
SELECT Account.Name FROM Opportunity WHERE (Name LIKE '%2016%') AND ((NOT Name LIKE '%2017%') OR (NOT Name LIKE '%2018%'))

Please mark this as the Best answer if this solves your issue.

Thanks