Print When Expression to print only the last eleme

2019-06-01 02:14发布

问题:

I have following data set in oracle table and want to generate CSV file from that data.

My data

CaseID  Amount
1000     10
1000     20
1000     50      
2000     30 
2000     10     
3000     30 
3000     20 
3000     20   

It is necessary to show the following output from above data.

Required Output

CaseID  Amount  Aggregate
1000     10
1000     20
1000     50      80

2000     30 
2000     10      40

3000     30 
3000     20 
3000     20      70

Aggregate column is populated by accumulating the sum of Amount according to caseID group.

Group summary should display on last element of the group. But not in the next row, should display in the same row and next column, that matching with the last element.

I tried with 'Print When Expression' for other scenarios, but unable to use with this scenario. Also I have find similar questions, but not cater with this requirement. So would like to know the possible solutions for this.

回答1:

You can do something similar with evaluationTime="Auto". You can have a text element that shows the value on the last record in the group and is empty for other records. It's not the same as not printing the element at all, but you can't use the print when expression because it doesn't have delayed evaluation.

evaluationTime="Auto" uses the reset types of variables to decide the moment at which it reads the variable values. Each group has an automatically created count variable that resets with the group, and if you create a new variable that resets on each record you can use it to determine if the current record is the last record in the group.

The whole report would look something like this

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="report" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" resourceBundle="jasperreports" whenResourceMissingType="Key" isIgnorePagination="true" uuid="816687ff-bb19-4f6b-a2b6-53634ce095fc">
    <style name="dd" isDefault="true" fontSize="10"/>
    <field name="CaseID" class="java.lang.Integer"/>
    <field name="Amount" class="java.lang.Integer"/>
    <variable name="AmountSum" class="java.lang.Integer" resetType="Group" resetGroup="CaseIDGroup" calculation="Sum">
        <variableExpression><![CDATA[$F{Amount}]]></variableExpression>
    </variable>
    <variable name="GroupCurrentCount" class="java.lang.Integer" resetType="None">
        <variableExpression><![CDATA[$V{CaseIDGroup_COUNT}]]></variableExpression>
    </variable>
    <group name="CaseIDGroup">
        <groupExpression><![CDATA[$F{CaseID}]]></groupExpression>
    </group>
    <detail>
        <band height="20">
            <textField>
                <reportElement x="0" y="0" width="100" height="20" uuid="af7a5ea9-ffcb-4a7f-aaa8-7d5cab06a579"/>
                <textFieldExpression><![CDATA[$F{CaseID}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="100" y="0" width="100" height="20" uuid="a7e2795e-456a-4c6e-946f-8315df453b1f"/>
                <textFieldExpression><![CDATA[$F{Amount}]]></textFieldExpression>
            </textField>
            <textField evaluationTime="Auto" isBlankWhenNull="true">
                <reportElement x="200" y="0" width="100" height="20" uuid="afda4fcc-78fc-46f9-8c5c-2e0c4f04dfa5"/>
                <textFieldExpression><![CDATA[$V{GroupCurrentCount}.equals($V{CaseIDGroup_COUNT}) ? $V{AmountSum} : null]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>