Search code examples
jasper-reports

Incorrect sum when grouped over multiple pages


I'm building a report with groups on JasperSoft Studio, and I'm trying to get the sum of values of two columns for each group. I'm able to successfully generate the report and am getting most of the right values, but I've noticed that each group's sums are accurate if and only if all the values in the group are on the same page, otherwise, I get incorrect sums.

This is the portion of the .jrxml that defines the group:

<group name="customer-tree">
    <groupFooter>
        <band height="30">
            <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
            <frame>
                <textField>
                    <reportElement x="1407" y="0" width="66" height="30" uuid="cd6ad251-ee00-480a-bab7-04e1b094448b"/>
                    <textElement textAlignment="Right">
                        <font isBold="true"/>
                    </textElement>
                    <textFieldExpression><![CDATA[$V{customer-tree_billing-total}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="1473" y="0" width="72" height="30" uuid="d2424c64-8449-4c75-aca8-69244b990d44"/>
                    <textElement textAlignment="Right" verticalAlignment="Top">
                        <font isBold="true"/>
                    </textElement>
                    <textFieldExpression><![CDATA[$V{customer-tree_total}]]></textFieldExpression>
                </textField>
            </frame>
        </band>
    </groupFooter>
</group>

And these are the definitions of the variables used to sum:

<variable name="customer-tree_total" class="java.lang.Long" resetType="Group" resetGroup="customer-tree" incrementType="Column" calculation="Sum">
    <variableExpression><![CDATA[$V{customer-tree_total} + $F{total_calls}]]></variableExpression>
    <initialValueExpression><![CDATA[0L]]></initialValueExpression>
</variable>
<variable name="customer-tree_billing-total" class="java.lang.Long" resetType="Group" resetGroup="customer-tree" incrementType="Column" calculation="Sum">
    <variableExpression><![CDATA[$V{customer-tree_billing-total} + $F{billing_days}]]></variableExpression>
    <initialValueExpression><![CDATA[0L]]></initialValueExpression>
</variable>

Things I've already tried:

  • Using different query parameters. I only see wrong values when all the results in one group can't fit onto one page, and I've confirmed this by viewing all the results on one page by adding <property name="net.sf.jasperreports.page.break.no.pagination" value="apply"/> to the source. In this case, all the group's sums are accurate.
  • Changing the parameter and related variables types all to java.lang.Integer (instead of java.lang.Long) to see if the <variableExpression> expressions would be affected.
  • Keeping the parameters as java.lang.Long and adding them to the related variables with Long's intValue().

Solution

  • Remove incrementType="Column" and leave only $F{..} in the variable expressions:

    <variable name="customer-tree_total" class="java.lang.Long" resetType="Group" resetGroup="customer-tree" calculation="Sum">
        <variableExpression><![CDATA[$F{total_calls}]]></variableExpression>
        <initialValueExpression><![CDATA[0L]]></initialValueExpression>
    </variable>
    <variable name="customer-tree_billing-total" class="java.lang.Long" resetType="Group" resetGroup="customer-tree" calculation="Sum">
        <variableExpression><![CDATA[$F{billing_days}]]></variableExpression>
        <initialValueExpression><![CDATA[0L]]></initialValueExpression>
    </variable>