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

SOQL Aggregate - Top 10 only, by Net Ordered Value


I have this SOQL query :

AggregateResult[] results = [
              SELECT ProductLine_Name__c        label,
              GROUPING(ProductLine_Name__c)     labelGroup,
              SUM(Net_Order_Value_CustCurr__c)  value,
              SUM(Ordered_Quantity__c)          qty
              FROM     ROS_Orders__c
              WHERE    Account__c = :id
              AND      REPORT_Is_thisSeason__c = 1
              AND      Type__c = 'Orders'
              GROUP BY CUBE(ProductLine_Name__c)];


How can I add an extra filter/sort expresison to return only the TOP 10 results by value (ie Net_Order_Value_CustCurr__c) ?


Thanks for any help,



Seems like if you grouped by Net_Order_Value_CustCurr__c too that you could determine the top 10 when looping through the aggregate results.


This should do it.


ORDER By Net_Order_Value_CustCurr__c Desc LIMIT 10


Looking at your query again, this will only give you the top 10 out of everything, greenstork is correct that you should be able to identify that within your loop.