IReport Group Expression Multiple Fields

ireportjasper-reports

How could i write a Group Expression in iReport with Multiple Fields ? Report output should be as below.

Buyer   Product Unit    Quantity    Total
------------------------------------------
Buyer2  Banana  Count    50
                                    50
Buyer2  Banana  Kg        5
Buyer2  Banana  Kg        5
                                    10
Buyer2  Coconut Count    20
                                    20
Buyer4  Papaya  Count   500
                                    500
Buyer4  Mango   Count   200
                                    200
Buyer5  Banana  Kg       15
Buyer5  Banana  Kg       15
                                     30

Best Answer

This simplest thing to do is create multiple groups. SO in your example you would use three groups, one for buyer, product, and unit. You can then use a variable to sum the quantity and have it reset with the unit group.

Below is a basic example JRXML, that you can adapt:

<?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="report1" language="groovy" pageWidth="612" pageHeight="792" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="b478862e-b118-4290-8664-eae9b2966b31">
    <parameter name="GROUP_BY" class="java.lang.String">
        <defaultValueExpression><![CDATA[$F{field1}]]></defaultValueExpression>
    </parameter>
    <field name="buyer" class="java.lang.String"/>
    <field name="product" class="java.lang.String"/>
    <field name="unit" class="java.lang.Number"/>
    <field name="quantity" class="java.lang.String"/>
    <variable name="sumQuantity" class="java.lang.Number" resetType="Group" resetGroup="unit" calculation="Sum"/>
    <group name="Buyer">
        <groupExpression><![CDATA[$F{buyer}]]></groupExpression>
    </group>
    <group name="product">
        <groupExpression><![CDATA[$F{product}]]></groupExpression>
    </group>
    <group name="unit">
        <groupExpression><![CDATA[$F{unit}]]></groupExpression>
        <groupFooter>
            <band height="26">
                <textField>
                    <reportElement uuid="d1f95335-0fee-4d75-ac0d-74f0dc478a78" x="400" y="0" width="100" height="20"/>
                    <textElement/>
                    <textFieldExpression><![CDATA[$V{sumQuantity}]]></textFieldExpression>
                </textField>
            </band>
        </groupFooter>
    </group>
    <columnHeader>
        <band height="21" splitType="Stretch">
            <staticText>
                <reportElement uuid="c4ee8763-fb32-4805-9bcd-4b407bd7ae35" x="0" y="0" width="100" height="20"/>
                <textElement/>
                <text><![CDATA[buyer]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="5c5e099b-f2b4-4c98-b428-bc3983711136" x="100" y="0" width="100" height="20"/>
                <textElement/>
                <text><![CDATA[product]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="1bff9ddd-fa17-4b7f-bc00-032653cf2307" x="200" y="0" width="100" height="20"/>
                <textElement/>
                <text><![CDATA[unit]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="94e75612-cee4-45e9-97c0-a0d1b8ac6269" x="300" y="0" width="100" height="20"/>
                <textElement/>
                <text><![CDATA[quantity]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="92e83a6d-198f-4ee1-95eb-295cb2415136" x="400" y="0" width="100" height="20"/>
                <textElement/>
                <text><![CDATA[total]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="23" splitType="Stretch">
            <textField>
                <reportElement uuid="927b66fa-f4aa-4acf-ac85-fde9164bc974" x="0" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{buyer}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="f5e8d24f-5529-489d-94b9-78b86b09adda" x="100" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{product}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="9c9f129b-64c7-40d5-a0a9-af2069d58296" x="200" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{unit}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="d9443c16-dcac-4a72-b85f-d961c215aee4" x="300" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{quantity}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>
Related Topic