I have similar code to this where I want to store the values of a query from a semi-join and anti-join. I want to store the values in a list respective on whether if its a semi-join or anti-join. But since both of them are nearly identical I don't want to call both at them twice.
Original
List<Account> inQuery = [SELECT Id, Name
FROM Account
WHERE Id IN
(SELECT AccountId
FROM Opportunity
WHERE StageName = 'Closed Lost')]
List<Account> notQuery = [SELECT Id, Name
FROM Account
WHERE Id NOT IN
(SELECT AccountId
FROM Opportunity
WHERE StageName = 'Closed Lost')]
// do something with inQuery
// do something with notQuery
I want to do the inner query once and add the values into each respective list. I was thinking of something like this but I cant figure out how to do it or if there is another way to approach it.
Modified
List<Opportunity> innerQuery = [SELECT AccountId FROM Opportunity WHERE StageName = 'Closed Lost'];
List<Account> inQuery = new List<Account>();
List<Account> notQuery = new List<Account>();
for (Account acc: [SELECT Id, Name FROM Account]) {
if() { // if some statement is true, then add to inQuery
}
else { // else add to notQuery
}
}
// do something with inQuery
// do something with notQuery