+ Start a Discussion
@altius_rup@altius_rup 

SOQL Aggregate - Top 10 only, by Net Ordered Value

Hi,

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,

Rup

greenstorkgreenstork

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.

RobertCRobertC

This should do it.

 

ORDER By Net_Order_Value_CustCurr__c Desc LIMIT 10

RobertCRobertC

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.