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
DatThepSoftDatThepSoft 

How can I get all records with only max "Version__c" in query statement return?

Example: I have a Object A with many fields: ID, Name, Version__c,DescriptionName__c

 

and Data here:

 

Record : Name                   Version__c                          DescriptionName__c

                Protocol1              1                                                //

               

                Protocol1              2                                                //

               

                Protcol 2               1                                              //

               

                Protocol3              1                                              //

               

                Protocol2               2                                              //

I want to get All Protocols have max value.

The result have to return : Protocol1 - version :2

                                               Protocol2- version: 2

                                               Protocol3 - version:1

Please help me,

datthepsoft

souvik9086souvik9086

LIST<AggregateResult> maxVer = new LIST<AggregateResult> ();
maxVer = [SELECT MAX(Version__c),name mxVr FROM ObjectA__c GROUP BY Name ];
for(AggregateResult sobj : maxVer){
String maxVersion = Integer.valueOf(sobj.get('name')) + Integer.valueOf(sobj.get('mxVr'));
}

 

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

DatThepSoftDatThepSoft

Thank you for your help! But I want to make query string dont have code behide. :D

Could you please help me?

souvik9086souvik9086

So your query string will be like this

 

LIST<AggregateResult> maxVer = new LIST<AggregateResult> ();
String soqlQuery = 'SELECT MAX(Version__c),name mxVr FROM ObjectA__c GROUP BY Name';

maxVer  = Database.query(soqlQuery);

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

 

Sonam_SFDCSonam_SFDC

Try the following query:

 

SELECT Name, DescriptionName__c,  MAX(Version__c)
FROM OBJECT_NAME WHERE Version__c !=NULL
GROUP BY Name

 

Let me know if it suits your requirement or needs change

DatThepSoftDatThepSoft

I can not get result with:

'Select ID,DescriptionName__c, Max(Version__c), name mxVr From ObjectA__c Group By Name'.

 

and see error: 

 

Field must be grouped or aggregated: DescriptionName__c


I want to get all field in Object. Could you please help me!

Thanks,

mnknmnkn

Like the error message says, you have to group the fields that are not aggregated.

Add these fields to the grouping.