iReport - 尝试根据具有至少一个值1的另一个组来计算组

时间:2012-07-23 00:14:58

标签: jasper-reports ireport

我正在尝试使用以下信息计算报告的值:

主题组有多个事件组,每个事件都有一个值为1或0的字段。我试图弄清楚如何计算至少有一个事件组的值为1的主题组的数量。

我尝试过拆分成多个变量,但似乎无法进行计算,也无法正确重置。我可以返回所有事件的计数以及所有具有1值的事件的总和。

我的数据显示5个值为1的事件,但我需要返回4个主题的数量。任何帮助都将非常感谢。

1 个答案:

答案 0 :(得分:0)

试试这个 - 这是2组和一个细节值。我们取内部组中的细节值的总和,然后计算内部组的总和>的值的计数。 0.将此项放入iReport时,请检查重置类型/组和增量类型/组。还有一些样本数据。

<?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="report3" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="9c7e3951-509f-4aa7-8f30-bc6196708d3d">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <queryString>
        <![CDATA[SELECT 'I1' AS GROUP_1,
  'J1' AS GROUP_2,
  0 AS DETAIL_VALUE
FROM DUAL

UNION

SELECT 'I1' AS GROUP_1,
  'J2' AS GROUP_2,
  1 AS DETAIL_VALUE
FROM DUAL

UNION

SELECT 'I2' AS GROUP_1,
  'J1' AS GROUP_2,
  1 AS DETAIL_VALUE
FROM DUAL

UNION

SELECT 'I2' AS GROUP_1,
  'J2' AS GROUP_2,
  1 AS DETAIL_VALUE
  FROM DUAL

UNION

SELECT 'I2' AS GROUP_1,
  'J3' AS GROUP_2,
  1 AS DETAIL_VALUE
FROM DUAL

UNION

SELECT 'I3' AS GROUP_1,
  'J1' AS GROUP_2,
  0 AS DETAIL_VALUE
FROM DUAL

UNION

SELECT 'I3' AS GROUP_1,
  'J2' AS GROUP_2,
  0 AS DETAIL_VALUE
FROM DUAL]]>
    </queryString>
    <field name="GROUP_1" class="java.lang.String"/>
    <field name="GROUP_2" class="java.lang.String"/>
    <field name="DETAIL_VALUE" class="java.math.BigDecimal"/>
    <variable name="GROUP_2_SUM" class="java.math.BigDecimal" resetType="Group" resetGroup="GROUP_1" calculation="Sum">
        <variableExpression><![CDATA[$F{DETAIL_VALUE} == 1 ? 1 : 0]]></variableExpression>
    </variable>
    <variable name="GROUP_1_TALLY" class="java.math.BigDecimal" resetType="Group" resetGroup="GROUP_1" calculation="Count">
        <variableExpression><![CDATA[$V{GROUP_2_SUM} > 0 ? 1 : 0]]></variableExpression>
    </variable>
    <group name="GROUP_1">
        <groupExpression><![CDATA[$F{GROUP_1}]]></groupExpression>
        <groupHeader>
            <band height="25">
                <textField>
                    <reportElement uuid="5216d0ad-f2f0-43ae-b344-95019a7a99eb" x="0" y="0" width="100" height="20"/>
                    <textElement/>
                    <textFieldExpression><![CDATA[$F{GROUP_1}]]></textFieldExpression>
                </textField>
            </band>
        </groupHeader>
        <groupFooter>
            <band height="25">
                <textField>
                    <reportElement uuid="e4ac638c-f609-45b1-94ba-27d9f5d9c28c" x="455" y="0" width="100" height="20"/>
                    <textElement/>
                    <textFieldExpression><![CDATA[$V{GROUP_2_SUM}]]></textFieldExpression>
                </textField>
            </band>
        </groupFooter>
    </group>
    <group name="GROUP_2">
        <groupExpression><![CDATA[$F{GROUP_2}]]></groupExpression>
        <groupHeader>
            <band height="25">
                <textField>
                    <reportElement uuid="437a9d72-5916-493a-b1f1-c582e2ad0f64" x="20" y="0" width="100" height="20"/>
                    <textElement/>
                    <textFieldExpression><![CDATA[$F{GROUP_2}]]></textFieldExpression>
                </textField>
            </band>
        </groupHeader>
        <groupFooter>
            <band height="25"/>
        </groupFooter>
    </group>
    <background>
        <band splitType="Stretch"/>
    </background>
    <detail>
        <band height="25" splitType="Stretch">
            <textField>
                <reportElement uuid="21cd5fb1-94a1-448e-ab53-0fa846fedee5" x="40" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{DETAIL_VALUE}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <summary>
        <band height="42" splitType="Stretch">
            <textField>
                <reportElement uuid="624d0f87-ce9a-44d6-8e2c-be0ec7e65da2" x="455" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$V{GROUP_1_TALLY}]]></textFieldExpression>
            </textField>
        </band>
    </summary>
</jasperReport>