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
Mario CMario C 

Grouping Fields in SOQL Query

The following query fetches the latest checkout date for each property among all products with status 'booked'.

The query returns the max date and related property ID. Is there a way to fetch the product ID (OpportunityLineItem ID) too? Unfortunatelly, if I add the Id to the query, I must also group by the Id field. 
SELECT MAX(End_Date_Time__c) latestCheckOut, Property__c Property
                      FROM OpportunityLineItem 
                      WHERE OpportunityId = :oli.OpportunityId AND Status__c = 'booked'
                      GROUP BY Property__c

User-added image
Raj VakatiRaj Vakati
If group by OpportunityLineItemid will not work then you need to write custom code to get the aggigate values