0

How to query opportunities that have a closedate in the past, but are within an account that has a closedate after Today?

Below is the query I'm trying to execute, but I get the following error:

select id 
from opportunity 
where closedate <= Today 
and accountid not in 
    (select accountid from opportunity where closedate >= Today)

Is it possible to do this?

1
  • what's the error you are getting when you run the query ? May 13, 2015 at 6:12

1 Answer 1

1

I don't think that's achievable by doing an inner select referencing another sObject (Account via AccountId). I would query the accounts first and put their IDs in a set that I can then use in the other query:

Set <Id> accountIds = new Set <Id> ();

for (Opportunity oppty : [select accountid from opportunity where closedate >= Today])
{
    accountIds.add(oppty.AccountId);
}

system.debug([select id from opportunity where closedate <= Today and accountid not in :accountIds]);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .