+ Start a Discussion
zeenat mangatzeenat mangat 

Error: Field must be grouped or aggregated: Id

Following is my code where I use query with group by cluse for sum up the quantity that has the same name.

public List<Order_Item__c> orders;
    public String email;
    public List<Order_Item__c> getOrders() {
        email ='se.dx@gmail.com';
        orders = [SELECT Chargent_Order__r.Id, Product_Code__c, SUM(Quantity__c), Chargent_Order__r.ChargentOrders__Total__c,           Product__r.Name, Chargent_Order__r.ChargentOrders__Billing_Email__c  FROM Order_Item__c WHERE Chargent_Order__r.ChargentOrders__Billing_Email__c =:email GROUP BY Product__r.Name];
   return orders;
    }

But it shows the above error please help me
Best Answer chosen by zeenat mangat
KaranrajKaranraj
If you are using aggregate function in SOQL query then you have to follow two procedure
1. Either you have keep the aggregate function only in the select statement SUM(Quantity__c)
2. If you including other fields as well in the select statement then you have to group by those fields in the SOQL
[SELECT  SUM(Quantity__c) FROM Order_Item__c WHERE Chargent_Order__r.ChargentOrders__Billing_Email__c =:email GROUP BY Product__r.Name]