I have arrived to the following SOQL:
select
Id,
OwnerId,
(SELECT id, ParentId FROM EmailMessages
where toLabel(Status) = 'New' and Incoming = true)
from Case
This query retrieves the Case ids, owners and EmailMessages related to them, due to the subselect having a where clause some records are null and that's OK, but it still retrieves ALL the cases. So, I wanted to know if there was a way to do any of the following:
- Limit the results only to cases with EmailMessages in that condition
- Group by OwnerId, counting the EmailMessages with that condition
- Use a semi-join with EmailMessages
Any of this would help me to limit the results and execution time significantly.
Thanks in advance!