+ Start a Discussion
Mike DeMilleMike DeMille 

SOQL Query - Need Help!

I don't know if this can be done, but I'm trying to get a count of tasks and events related to either an account or contact under an account.  

I was hoping something like: 

SELECT Id 
   FROM Account
       (SELECT COUNT(Id) 
           FROM  TASK)

I'm basically trying to see how many tasks and events are related to an account (through account or account contacts).  My query doesn't recognize 'TASK' in this query, plus I am missing many other things like how to query for events also.  

Can anyone help point me in the right direction? I'm just running this from workbench
Best Answer chosen by Mike DeMille
Alain CabonAlain Cabon
You could try the following request:

select whatid,what.name, count(id) from task group by whatid,what.name

select whatid,what.name, count(id) from event group by whatid,what.name

 

All Answers

Alain CabonAlain Cabon
You could try the following request:

select whatid,what.name, count(id) from task group by whatid,what.name

select whatid,what.name, count(id) from event group by whatid,what.name

 
This was selected as the best answer
Mike DeMilleMike DeMille
I see that you are unable to count in subqueries.  
Alain CabonAlain Cabon
=> only root queries support aggregate expressions:

SELECT Id ,  (SELECT COUNT(Id)  FROM  TASKS)   // NOT CORRECT
   FROM Account
  
But the following request is correct:

SELECT Id ,  (SELECT Id  FROM  TASKS)
   FROM Account

Notice the "S" at the end of TASK.

That was your first idea but it is not allowed in SOQL.