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
Jack A 2Jack A 2 

How to Compare SOQL Query Result

For Example:
for(Contact con : [Select Id,Salary__c From Contact Where AccountId = :accId Limit 100]){
            System.debug('Check min'+con.Salary__c);
 }
Here I am getting all the salary from contact, now how to compare those and show minimum of them.
FYI:  I know using AggregateResult is more efficient.
 
Best Answer chosen by Jack A 2
Siddharth83JainSiddharth83Jain
@jack A 2, My approach is to show just the minimum value without querying all. But you need to show all the records and highlight the minimum then you can simple add a where clause. Try something similar to below and then you can have an identifier in the code initialized to 0 and compare the value or straight away pick the first record and break the loop.
 
for(Contact con : [Select Id,Salary__c From Contact Where AccountId = :accId and Salary != null order by Salary__c asc Limit 100]){
            
 }
Let me know if that help, please mark this as best answer.

Thanks
Siddharth
OSI Consulting
 

All Answers

Siddharth83JainSiddharth83Jain
Hi Jack, 

Use the aggregate functions available in SFDC and use MIN() function to get the desired result.

SELECT MIN(CreatedDate), FirstName, LastName FROM Contact GROUP BY FirstName, LastName

Let me know if you need further inputs. If this helps please mark this as best answer.

Thanks
Siddharth
OSI Consulting
Jack A 2Jack A 2
@Sidharth83Jain I am getting all the salary of all contact in above for loop, now what I want to know how to compare those and show minimum among them.
Siddharth83JainSiddharth83Jain
@jack A 2, My approach is to show just the minimum value without querying all. But you need to show all the records and highlight the minimum then you can simple add a where clause. Try something similar to below and then you can have an identifier in the code initialized to 0 and compare the value or straight away pick the first record and break the loop.
 
for(Contact con : [Select Id,Salary__c From Contact Where AccountId = :accId and Salary != null order by Salary__c asc Limit 100]){
            
 }
Let me know if that help, please mark this as best answer.

Thanks
Siddharth
OSI Consulting
 
This was selected as the best answer
Jack A 2Jack A 2
Got it @Sid Thanx!