How to Choose the Initial Record from a Set of Multiple Records


I’m looking for some assistance in a simple SOQL statement. I haven’t written SQL for over 15 years (so a real newbie to SOQL)

Looking for what i believe must be a simple SOQL statement. I used SQL many years ago but now have an urgent need to get my data volumes down, fast.

I have multiple records in a table and I only want to select the latest record for each client.

I was thinking it would be easy using the sorted data (by date record added) and then using the First function. But it lt appears the First function is very different from SQL to SOQL

for example the data looks like this:

  • Cln_Id, Client_name, dte_attach_added
  • 00001 Mary Blogs 13/01/2014
  • 00001 Mary Blogs 29/02/2013
  • 00001 Mary Blogs 15/03/2015
  • 00341 Billy Bobs 07/11/2014
  • 00789 Wendy Woo 09/10/2014
  • 00789 Wendy Woo 15/11/2014


you can use ORDER BY in your SOQL to first sort the records by attachment date in descending order (so that you get latest -> oldest) and then use LIMIT to get only the top 1 record which is the latest..

Select Id, Name from YourObject Order By AttachmentDateField__c Desc Limit 1Select Id, Name from YourObject Order By AttachmentDateField__c Desc Limit 1

in the above SOQL, replace YourObject with your actual object name and AttachmentDateField__c with your actual attachment date field name and include any additional fields you want to retrieve.