使用iReport获得前5名记录的总计

时间:2013-04-30 17:02:04

标签: jasper-reports ireport

我使用 iReport版本4.0.1 显示前5条记录,我想只对前5条记录求和。

我尝试使用累积方法并且它有效,但它显示了5条记录中每条记录的累计总数,而我只需要5条记录的总计。任何帮助?

1 个答案:

答案 0 :(得分:0)

在摘要变量中,尝试将变量表达式设置为:

 $V{REPORT_COUNT} <= 5 ? $F{my_value} : 0

并将计算设置为Sum。

以下是一些适合我的示例代码:

    <?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="average_of_averages" language="groovy" pageWidth="612" pageHeight="792" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="b9853d5e-8a8a-4721-9db0-5ea8f8a25db5">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <queryString>
        <![CDATA[SELECT * FROM (
SELECT
'Set_1' AS myset
, 50 AS myscore
FROM dual
UNION ALL
SELECT
'Set_1' AS myset
, 25 AS myscore
FROM dual
UNION ALL
SELECT
'Set_1' AS myset
, 75 AS myscore
FROM dual
UNION ALL
SELECT
'Set_1' AS myset
, 95 AS myscore
FROM dual
UNION ALL
SELECT
'Set_1' AS myset
, 35 AS myscore
FROM dual
UNION ALL
SELECT
'Set_1' AS myset
, 15 AS myscore
FROM dual
UNION ALL
SELECT
'Set_1' AS myset
, 65 AS myscore
FROM dual
UNION ALL
SELECT
'Set_1' AS myset
, 10 AS myscore
FROM dual
UNION ALL
SELECT
'Set_1' AS myset
, 95 AS myscore
FROM dual
)
ORDER BY myset]]>
    </queryString>
    <field name="MYSET" class="java.lang.String"/>
    <field name="MYSCORE" class="java.math.BigDecimal"/>
    <variable name="mysetsum" class="java.math.BigDecimal" calculation="Sum">
        <variableExpression><![CDATA[$V{REPORT_COUNT} <= 3 ? $F{MYSCORE} : 0]]></variableExpression>
        <initialValueExpression><![CDATA[0]]></initialValueExpression>
    </variable>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="25" splitType="Stretch">
            <textField>
                <reportElement uuid="1ae3a434-c464-49ec-a295-e9e1472b31fd" x="0" y="0" width="572" height="25"/>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA["Top 3 Sum"]]></textFieldExpression>
            </textField>
        </band>
    </title>
    <detail>
        <band height="20" splitType="Stretch">
            <staticText>
                <reportElement uuid="6cbc371f-144c-4cb7-be0e-55d588c0f437" x="50" y="0" width="100" height="20"/>
                <textElement/>
                <text><![CDATA[Score:]]></text>
            </staticText>
            <textField>
                <reportElement uuid="4c311c34-5fe0-404e-bcd0-d2e982cdaaa5" x="150" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{MYSCORE}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <summary>
        <band height="42" splitType="Stretch">
            <textField pattern="###0.00">
                <reportElement uuid="9c17a4a2-8f0c-4b5d-8fa1-2457dae948d6" x="150" y="10" width="100" height="20"/>
                <textElement>
                    <font size="12" isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA[$V{mysetsum}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement uuid="6cbc371f-144c-4cb7-be0e-55d588c0f437" x="0" y="10" width="150" height="20"/>
                <textElement>
                    <font size="12" isBold="true"/>
                </textElement>
                <text><![CDATA[Top 3 Sum:]]></text>
            </staticText>
        </band>
    </summary>
</jasperReport>