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
Varun AnnadataVarun Annadata 

how to use group by in this soql query?

          List<Program_Member_Stage_MVN__c>Pms =[select id,Name,Parent_Program_Member_Stage_MVN__c,Program_Stage_MVN__c,Status_MVN__c,Program_Stage_Name_MVN__c,Stage_Sequence_Number_MVN__c,Activity_Sequence_Number_CTS__c,Parent_Program_Stage__c from Program_Member_Stage_MVN__c  where Program_Member_MVN__c=:Pm.id and Status_MVN__c='Not Started' and Parent_Stage__c !='Parent_Stage_MVN' order by Parent_Program_Stage__c,Activity_Sequence_Number_CTS__c]; 

I am getting this error:
 Field must be grouped or aggregated: Id at line 18 column 49.How to bypass this error?
Sukanya BanekarSukanya Banekar
Hi Varun,
Write the field after select statetement on which you want to make group by for e.g
SELECT  Parent_Program_Member_Stage_MVN__c,Activity_Sequence_Number_CTS__c from Program_Member_Stage_MVN__c   group by Parent_Program_Member_Stage_MVN__c;

Let me know if this solves you problem.

Thanks,
Sukanya Banekar
Amol Salve 13Amol Salve 13
Hello Annnadata,
      If you have to use Group by clouse you must have to use aggreaget function like min, max,count, sum. Field which you used in group by clouse must used aggregate function.here is small example 

SELECT Description,  count(Id), COUNT_DISTINCT(WhatId) FROM Task WHERE WhoId = :contactId GROUP BY Description

Thank you,\
Amol Salve
Salesforce developer
Mustafa JhabuawalaMustafa Jhabuawala
Varun,

As its is mentioned in the error itself "Field must be grouped or aggregated"

You need to write an aggregate function in your query to make it function.

For ex - 
Select MIN(Id), Name from Account group by Name
Select COUNT(Id), Name from Account group by Name