這很容易。您可以在產品字段和該組上的兩個變量上創建組:計算結果總數和平均總和。在內置變量的幫助下,您可以計算並顯示該組中元素的數量。
這是工作示例:
<?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>
此示例採用CSV文件作爲數據源。您可以編輯查詢 - 用SQL查詢獲取數據。
報告設計iReport的是:
其結果將是(通過預覽在iReport的):
你應該閱讀這篇文章using variables in JasperReports和這篇文章約using groups in JasperReports。
請記得您應該在組使用前對數據進行排序。 從後約分組和從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.
謝謝!我錯過的伎倆是小組樂隊。我一直在努力的關於iReport的部分是樂隊。相同的變量將根據它進入的頻帶顯示不同的結果。爲什麼數據是否被排序很重要? – Marianna 2012-07-17 22:55:51
作爲我原來的問題的附錄...是否有一種方法來使用我的總和乘數?我有一個額外的列,狀態,表明它的購買狀態。我有一個STATE_SALES_TAX表,我想查詢每條記錄。如果在新澤西州購買我的牛奶,銷售稅是1.06。我在紐約買了另外一加侖,它是1.08。有沒有一種方法可以用另一個表中的乘數來總結花在牛奶上的總花費?花在牛奶上的總金額= sum(eachMilkPx * salesTax(statePurchased)) – Marianna 2012-07-18 14:11:10
是的,您可以創建任何您需要的變量表達式。 – 2012-07-18 14:20:16