I want to be able to return the the top 10 groups (referred to in syntax as subgroup) of product based on sales year on year. My SOQL looks like this

Select fiscal_year(Despatch_Note__r.Invoice_Date__c) Year, Part_Number__r.subgroup__c, SUM(Nett_Value__c) 
From Despatch_Lines__c 
Where Despatch_Note__r.Customer_Account__r.name = 'ACME HARDWARE LTD' 
GROUP BY Part_Number__r.subgroup__c, fiscal_year(Despatch_Note__r.Invoice_Date__c) 
ORDER BY Part_Number__r.subgroup__c, Sum(Nett_Value__c) DESC 

My results are not what I wanted. It shows me the first 20 subgroups ordered alphabetically as opposed by nettvalue.

1 Answer 1


Are you sure your problem isn't simply the list of order by columns? Just put the sum first, so

ORDER BY Sum(Nett_Value__c) DESC, Part_Number__r.subgroup__c

It works for me as expected.

  • You might need to have a look at the first item in my question: "top 10 groups of product based on sales year on year". I need the following: GroupL:2012:2K, GroupL:2013:100K, GroupA:2012:20K, GroupA:2013:23K which is showing sub-group ordered by Nett_value__c DESC for 2013, groups stay together and ordered by year ascending Your syntax will order it as follows: GroupB:2013:100K GroupA:2013:23K GroupA:2012:20K GroupB:2012:2K
    – Bartley
    Commented Nov 19, 2013 at 15:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .