How to sum specific rows in iReport with query?

Marianna picture Marianna · Jul 16, 2012 · Viewed 17.2k times · Source

I'm trying to create a summary table using iReport. My dataset is returning a list of purchases made and prices. Something like

Milk, $1.23

Chicken, $5.45

Milk, $1.44

and so on. I want my table to be able to breakdown my item by product. I want a table with columns:

  • # of times product was purchased (#rows where product milk),
  • total dollar amount spent on the product (sum of prices where product is milk),
  • and average price of product (column 2 divided by column 1).

How can I do this? I have been playing around with variables and I can get a total sum of all prices, but I don't know how to do this with a subset of the data using a more complex query.

Answer

Alex K picture Alex K · Jul 17, 2012

It is quite easy. You can create group on product field and two variables on this group: for counting result sum and average sum. With help of built-in variable you can calculate and display the number of elements in this group.

This is working sample:

<?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="group_average2" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="product" class="java.lang.String"/>
    <field name="price" class="java.lang.Integer"/>
    <sortField name="product"/>
    <variable name="totalSum" class="java.lang.Double" resetType="Group" resetGroup="productGroup" calculation="Sum">
        <variableExpression><![CDATA[$F{price}]]></variableExpression>
        <initialValueExpression><![CDATA[0]]></initialValueExpression>
    </variable>
    <variable name="averageSum" class="java.lang.Double" resetType="Group" resetGroup="productGroup" calculation="Average">
        <variableExpression><![CDATA[$F{price}]]></variableExpression>
        <initialValueExpression><![CDATA[0]]></initialValueExpression>
    </variable>
    <group name="productGroup">
        <groupExpression><![CDATA[$F{product}]]></groupExpression>
        <groupFooter>
            <band height="20">
                <textField>
                    <reportElement x="0" y="0" width="122" height="20"/>
                    <box leftPadding="10" rightPadding="10">
                        <topPen lineWidth="1.0"/>
                        <leftPen lineWidth="1.0"/>
                        <bottomPen lineWidth="1.0"/>
                        <rightPen lineWidth="1.0"/>
                    </box>
                    <textElement/>
                    <textFieldExpression><![CDATA[$F{product}]]></textFieldExpression>
                </textField>
                <textField pattern="###0.00;-###0.00">
                    <reportElement x="244" y="0" width="122" height="20"/>
                    <box leftPadding="10" rightPadding="10">
                        <topPen lineWidth="1.0"/>
                        <leftPen lineWidth="1.0"/>
                        <bottomPen lineWidth="1.0"/>
                        <rightPen lineWidth="1.0"/>
                    </box>
                    <textElement textAlignment="Right"/>
                    <textFieldExpression><![CDATA[$V{totalSum}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="122" y="0" width="122" height="20"/>
                    <box leftPadding="10" rightPadding="10">
                        <topPen lineWidth="1.0"/>
                        <leftPen lineWidth="1.0"/>
                        <bottomPen lineWidth="1.0"/>
                        <rightPen lineWidth="1.0"/>
                    </box>
                    <textElement/>
                    <textFieldExpression><![CDATA[$V{productGroup_COUNT}]]></textFieldExpression>
                </textField>
                <textField pattern="###0.00;-###0.00">
                    <reportElement x="366" y="0" width="149" height="20"/>
                    <box leftPadding="10" rightPadding="10">
                        <topPen lineWidth="1.0"/>
                        <leftPen lineWidth="1.0"/>
                        <bottomPen lineWidth="1.0"/>
                        <rightPen lineWidth="1.0"/>
                    </box>
                    <textElement textAlignment="Right"/>
                    <textFieldExpression><![CDATA[$V{averageSum}]]></textFieldExpression>
                </textField>
            </band>
        </groupFooter>
    </group>
    <pageHeader>
        <band height="19">
            <staticText>
                <reportElement x="0" y="-1" width="122" height="20"/>
                <box leftPadding="10" rightPadding="10">
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle" markup="none">
                    <font isBold="true" isItalic="true"/>
                </textElement>
                <text><![CDATA[Product]]></text>
            </staticText>
            <staticText>
                <reportElement x="122" y="-1" width="122" height="20"/>
                <box leftPadding="10" rightPadding="10">
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle" markup="none">
                    <font isBold="true" isItalic="true"/>
                </textElement>
                <text><![CDATA[Number of orders]]></text>
            </staticText>
            <staticText>
                <reportElement x="244" y="-1" width="122" height="20"/>
                <box leftPadding="10" rightPadding="10">
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle" markup="none">
                    <font isBold="true" isItalic="true"/>
                </textElement>
                <text><![CDATA[Total sum]]></text>
            </staticText>
            <staticText>
                <reportElement x="366" y="-1" width="149" height="20"/>
                <box leftPadding="10" rightPadding="10">
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle" markup="none">
                    <font isBold="true" isItalic="true"/>
                </textElement>
                <text><![CDATA[Average sum of order]]></text>
            </staticText>
        </band>
    </pageHeader>
</jasperReport>

This sample takes CSV file as datasource. You can edit query - for getting data with SQL query.

The report design in iReport is:

The report design

The result will be (via preview in iReport):

The result in iReport

You should read this article about using variables in JasperReports and this post about using groups in JasperReports.

Please remember that you should sort the data before group using. The quote from the post about grouping and sorting from the jasperforge.org:

In order to get an accurate data representation, the data in the data source should be already ordered according to the group expressions used in the report. One can either perform data sorting through the report query, or use the <sortField/> element.