Skip to main content
Good Evening, 

I am using AggregateResult and GROUP BY ROLLUP to output some data.  After a long slog, I've got it working the way I need it to... except for one thing.  The "Total" row is showing at the top of the list instead of the bottom when I put the data into a table. 

Would anybody be able to help me get my total row so that it is the last item in the table?

Here is my VF page: 

<apex:page controller="CurrentWeekDY">

<apex:pageBlock title="Delivery WTD">

<apex:pageBlockTable value="{!DelSumOut}" var="dy">

<apex:column value="{!dy.Campaign}" headerValue="Campaign" />

<apex:column value="{!dy.Delivery}" headerValue="Delivery" />

</apex:pageBlockTable>

</apex:pageBlock>

And here is my controller: 

public class CurrentWeekDY {

public class DelSum {

public String Campaign {get; set;}

public String Delivery {get; set;}

public DelSum(string c, string d) {

this.Campaign = c;

this.Delivery = d;

}

}

public List<DelSum> DelSumList = new List<DelSum>();

public List<DelSum> getDelSumOut() {

AggregateResult[] AgR = [SELECT Camp__c, SUM(Spend__c) FROM TL_Client__c WHERE CWDelivery__c = TRUE GROUP BY ROLLUP(Camp__c) ORDER BY Camp__c];

for (AggregateResult DYList : AgR) {

DelSumList.add(new DelSum(String.valueOf(DYList.get('Camp__c')), String.valueOf(DYList.get('expr0'))));

}

return DelSumList;

}

}

I would really appreciate any help!!

Thanks, 

 

John 

6 answers
  1. May 13, 2024, 6:48 AM

    So, if anyone is still looking for an answer to this question, make sure you add an ORDER BY clause that targets your GROUP BY field and add NULLS LAST. Here's an example: 

    SELECT StageName, COUNT(Id), FORMAT(SUM(Amount)), FORMAT(AVG(Amount))

    FROM Opportunity

    GROUP BY ROLLUP (StageName)

    ORDER BY StageName NULLS LAST

    This will ensure the "Total" row is last (since the StageName in the case above will be null on that row -- and by default, nulls come first unless NULLS LAST is specified).

0/9000