Display sum in a group header

I'd like to print the sum of values before they are displayed. For example suppose I had the query:

<br />

SELECT branch,day,sales_amt<br />

FROM sales<br />

WHERE branch='Toys' OR branch='Auto'<br />

</td></tr></tbody></table><br />

which returns :<br />

<table align="center" border="0" cellpadding="3" cellspacing="1" width="90%"><tbody><tr><td><b>Code:</b></td></tr><tr><td><pre><br />

BRANCH DAY SALES_AMT<br />

Toys   1   200<br />

Toys   2   300<br />

Toys   3   400<br />

Auto   1   100<br />

Auto   2   200<br />

Auto   3   300<br />

</td></tr></tbody></table><br />

I'd like to display<br />

<table align="center" border="0" cellpadding="3" cellspacing="1" width="90%"><tbody><tr><td><b>Code:</b></td></tr><tr><td><pre><br />

<b>Toys 900</b><br />

   1 200<br />

   2 300<br />

   3 400<br />

<b>Auto 600</b><br />

   1 100<br />

   2 200<br />

   3 300<br />

</td></tr></tbody></table><br />

<br />

If I group by branch, I can create a variable which sums the sales_amt and resets for the group. The problem is the variable will only total after the last day is reached, making it possible to put the total in the group footer, but not the group header. Is there any way to achieve this?<br />

<br />

Note: I cannot change the query to include the sum. I have to do it in Jasper.<br />

<br />

Thanks!

STEPS:

 

1.Create a Varible with calculation type "Sum"

  

<variablename="quantitySumInGroup"class="java.lang.Integer"resetType="Group"resetGroup="positionNoGroup"calculation="Sum">
<variableExpression><![CDATA[$F{QUANTITY}]]></variableExpression>
<initialValueExpression><![CDATA[0]]></initialValueExpression>
</variable>

Set its variableExpression and initialValueExpression.


2.Make its reset type "Group" and set its reset group appropriately

Put the varible in the "Group Header" band and then set its Eval time to "Group" and its Eval Group to the appropriate group

The parts he put in bold are the important ones.

The problem in your logic was here: "The problem is the variable will only total after the last day is reached, making it possible to put the total in the group footer, but not the group header."

This statement can be corrected like this, "The variable will only total after the last day is reached, making it possible to put the total in the the group header only if the Evaluation Time of the field is set to Group (and the Evaluation Group is set appropriately)."

Leaving the Evaluation Time at its default value of "Now" will indeed cause the problems that you are running into.

你可能感兴趣的:(display)