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
Tobias Letzner 5Tobias Letzner 5 

Field must be grouped or aggregated: Name ??

Hii,
I want to simply find the number of Contacts linked with each account. So i use aggregate query for this. I want to debug Account name with number of contacts. But it shows "Field must be grouped or aggregated: Name" and i have already group it by accountid. 
My query is given below-

list<sobject> totalContactVsAccount=[select count(id) totalContact,accountid,account.name from contact where  accountid!=null group by accountid limit 10000];
        for(sobject obj:totalContactVsAccount){
            system.debug(obj);
        }
Best Answer chosen by Tobias Letzner 5
Malika Pathak 9Malika Pathak 9

Hi Tobias Letzner,

When you want to find the field from the aggregate query you have to put that field in the group by clause separated by a comma.
Here I will show you how to do it.

list<sobject> totalContactVsAccount=[select count(id) totalContact,accountid,account.name from contact where  accountid!=null group by accountid,Account.name limit 10000];

Now, this query will work and find the account name as well, and if you want more account fileds the put those fields by comma-separated.

if you having a problem then Ask me 
Please mark Best Answer If Your Problem Is Solved
thanks, Regards,
Malika Pathak

 

All Answers

Malika Pathak 9Malika Pathak 9

Hi Tobias Letzner,

When you want to find the field from the aggregate query you have to put that field in the group by clause separated by a comma.
Here I will show you how to do it.

list<sobject> totalContactVsAccount=[select count(id) totalContact,accountid,account.name from contact where  accountid!=null group by accountid,Account.name limit 10000];

Now, this query will work and find the account name as well, and if you want more account fileds the put those fields by comma-separated.

if you having a problem then Ask me 
Please mark Best Answer If Your Problem Is Solved
thanks, Regards,
Malika Pathak

 

This was selected as the best answer
rakesh kumar 668rakesh kumar 668
You just need to use the agrregate function as the error describes
 
Select Id,SUM(ReleaseAmount__c) from myObj__c Where nObj__c = 'a0DA00000012fbMMAQ' group by Id
 
That is it. Hope this is what you are looking for
cinema hd (http://www.cinemahdapk.info/)
James Wilson 70James Wilson 70
Select Id,SUM(ReleaseAmount__c) from myObj__c Where nObj__c = 'a0DA00000012fbMMAQ' group by Id this worked out pretty clean for me. thanks.
cinema hd https://cinemahdapk.co/