(Mode = the value that occurs most frequently in a given set of data.)
I have a Lookup field on Contacts which lists the Contact's Specialty. I'd like to find the mode of the Specialties of all Contacts for an Account and stamp that value on the Account. (This will run in batch job daily btw)
Here's what I have so far:
Initial Query: SELECT Id,AccountId, SpecialtyLookup__r.Name FROM Contact WHERE SpecialtyLookup__c != null
- Put the Account IDs in a Map
- For each Account in the Map find the mode of the related specialties
2.1 If there is a tie sort the list of the ties in alphabetical order and take the first option
- Stamp the winning Specialty in the Specialty__c field on the Account
Thoughts?