function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Carlos Sabado CortesCarlos Sabado Cortes 

How to use Agregated query in child soql?

Hello,
I am trying to create a soql like this:
SELECT Id, attr1, attr2, attr3
  FROM apiObject
  WHERE attr1 IN 
    (SELECT attr1 FROM apiObject  WHERE attr1 != null GROUP BY attr1, attr2, attr3 HAVING count(id) > 1)

But it shows this excepction:
ERROR at Row:2:Column:79
expecting a right parentheses, found 'GROUP'

Anybody could help me?
 
NagendraNagendra (Salesforce Developers) 
Hi Carlos,

Sorry for this issue you are encountering.

May I suggest you please check with below links which has the detailed explanation on how to build and use aggregate functions. Please let us know if this helps.

Kindly mark this as solved if the reply was helpful.

Thanks,
Nagendra
Ajay K DubediAjay K Dubedi
Hi Carlos,

You can try the following Query :
 
SELECT attr1, SUM(attr2) sumattr2, attr3,COUNT(id) Total FROM apiObject where attr1 != null GROUP BY attr1 HAVING count(ID) >1
SELECT StageName,SUM(Amount) OppAmount, COUNT(id) Total FROM Opportunity where StageName != null GROUP BY StageName HAVING count(ID) >1

Please mark as best answer if it helps you.

Thank You 
Ajay Dubedi

 
Carlos Sabado CortesCarlos Sabado Cortes
Hi all,
Thank you for the anwser but they aren´t the solution. The target for this query is retrieve id from aggregate query. In first time i developed a batch with this query " (SELECT attr1 FROM apiObject  WHERE attr1 != null GROUP BY attr1, attr2, attr3 HAVING count(id) > 1)" and in the excute i called this I am trying to create a soql like this: "SELECT Id, attr1, attr2, attr3  FROM apiObject  WHERE attr1 IN ;setID", but the problem is the limits because sometimes the 2º query returns > 50k rows, i have thought in create a second batch but i don´t like this solution.
Thank you for you help.
Carlos