I have just created a query that gets the latest entry for every client in a custom object (see this question, with a great answer from @eyescream).
SELECT id,
(SELECT score__c,
date__c
FROM my_custom_objects__r
ORDER BY date__c DESC
LIMIT 1)
FROM Client__c
WHERE id IN (SELECT client__c FROM my_custom_object__c)
I now need to create a query that selects the lowest score from the same object, but it can't be the latest entry, as I need to calculate the distance between lowest and latest. I know I can do this to get the lowest score:
SELECT id,
(SELECT score__c,
date__c
FROM my_custom_objects__r
ORDER BY score__c ASC
LIMIT 1)
FROM Client__c
WHERE id IN (SELECT client__c FROM my_custom_object__c)
However in some cases the latest entry and the lowest score are the same entry, which I am trying to avoid. I have tried to get my head around how to get the lowest score that is not the latest entry. I tried using the first query in a "NOT IN" clause in the second, but I cannot get that to work.
Any help would be greatly appreciated.