+ Start a Discussion
Arthur Santos AlmeidaArthur Santos Almeida 

The equivalent of "distinct" in SOQL does not work

I need to run a query but do not must return account id duplicated.

I followed these articles:
  • https://developer.salesforce.com/forums/?id=906F00000008yzrIAA
  • https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_groupby.htm
and this is my query: 
SELECT Id, AccountId FROM Asset GROUP BY AccountId

but this gives an error: 
[MALFORMED_QUERY] Field must be grouped or aggregated: Id

what do I need to change in my query to she works?
Best Answer chosen by Arthur Santos Almeida
Arthur Santos AlmeidaArthur Santos Almeida
Jigar Patel 31 and mukesh gupta thank you for your answer, it all works but I need Id Asset too, this works: 
SELECT Max(Id), AccountId FROM Asset GROUP BY AccountId
 

All Answers

Jigar Patel 31Jigar Patel 31
You have to remove id from selection since id is unique key which not allow you to select unique accountid. 
please try SELECT AccountId FROM Asset GROUP BY AccountId
 
mukesh guptamukesh gupta
Hi Arthur,

Please use below line:-
 
SELECT  AccountId, COUNT(Id) FROM Asset GROUP BY AccountId
if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 

 
Arthur Santos AlmeidaArthur Santos Almeida
Jigar Patel 31 and mukesh gupta thank you for your answer, it all works but I need Id Asset too, this works: 
SELECT Max(Id), AccountId FROM Asset GROUP BY AccountId
 
This was selected as the best answer