抑制组中的重复值

Suppressing duplicate values in groups

我有一份包含 2 个波段的报告:

  1. 用于显示具有 Group=N 的行的详细信息带,即如果 Group <> N,则它是隐藏的
  2. 组页脚,用于显示具有 Group=Y 的行的集合,即如果 Group <> Y,它是隐藏的

两个波段都显示日期、姓名和金额。组页脚聚合了 Amount 字段。 我想禁止重复日期。

例如,给定以下数据:

+------------+------+-------+--------+
|    Date    | Name | Group | Amount |
+------------+------+-------+--------+
| 2020-06-01 | A    | Y     |      5 |
| 2020-06-01 | A    | Y     |     10 |
| 2020-06-01 | D    | N     |      2 |
| 2020-06-01 | Z    | Y     |      4 |
| 2020-06-02 | B    | N     |      1 |
| 2020-06-02 | G    | Y     |      6 |
| 2020-06-02 | G    | Y     |      3 |
+------------+------+-------+--------+

应该显示:

+------------+------+--------+
|  Date      | Name | Amount |
+------------+------+--------+
| 2020-06-01 | A    |     15 |
|            | D    |      2 |
|            | Z    |      4 |
| 2020-06-02 | B    |      1 |
|            | G    |      9 |
+------------+------+--------+

使用 isPrintRepeatedValues="false",它会复制日期,因为它们在不同的范围内:

+------------+------+--------+
|  Date      | Name | Amount |
+------------+------+--------+
| 2020-06-01 | A    |     15 |
| 2020-06-01 | D    |      2 |
|            | Z    |      4 |
| 2020-06-02 | B    |      1 |
| 2020-06-02 | G    |      9 |
+------------+------+--------+

添加一个:

<group name="DateGroup">
        <groupExpression><![CDATA[$F{Date}]]></groupExpression>
 </group>

并使用以下表达式抑制日期字段:

<printWhenExpression><![CDATA[$V{DateGroup_COUNT} == 1]]</printWhenExpression>

也不起作用,如下图所示。我已经包含了用于调试的组和 DateGroup_Count 值:

+------------+------+--------+-------+-----------------+
|    Date    | Name | Amount | Group | DateGroup_COUNT |
+------------+------+--------+-------+-----------------+
|            | A    |     15 | Y     |               2 |
|            | D    |      2 | N     |               3 |
|            | Z    |      4 | Y     |               4 |
| 2020-06-02 | B    |      1 | N     |               1 |
|            | G    |      9 | Y     |               3 |
+------------+------+--------+-------+-----------------+

2020-06-01 应该显示在第一行,但在上面的 DateGroup_COUNT 值表示聚合带中的最后一行,因此 Date 字段在不应该显示的时候被抑制了.

请注意,以上可能在单个频段中实现。它代表报告的简化版本,无法使用单个波段实现,因为它:

用于数据源的 CSV:

Date,Name,Group,Amount
2020-06-01,A,Y,5
2020-06-01,A,Y,10
2020-06-01,D,N,2
2020-06-01,Z,Y,4
2020-06-02,B,N,1
2020-06-02,G,Y,6
2020-06-02,G,Y,3

JRXML

<?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="duplicatetest" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="datasuppressiontestdata"/>
    <field name="Date" class="java.util.Date"/>
    <field name="Name" class="java.lang.String"/>
    <field name="Group" class="java.lang.String"/>
    <field name="Amount" class="java.math.BigDecimal"/>
    <variable name="groupTotal" class="java.math.BigDecimal" resetType="Group" resetGroup="Aggregate" calculation="Sum">
        <variableExpression><![CDATA[$F{Amount}]]></variableExpression>
    </variable>
    <group name="DateGroup">
        <groupExpression><![CDATA[$F{Date}]]></groupExpression>
    </group>
    <group name="Aggregate">
        <groupExpression><![CDATA[$F{Group}]]></groupExpression>
        <groupFooter>
            <band height="30">
                <printWhenExpression><![CDATA[EQUALS($F{Group}, "Y")]]></printWhenExpression>
                <textField evaluationTime="Group" evaluationGroup="DateGroup" pattern="yyyy-MM-dd">
                    <reportElement isPrintRepeatedValues="false" x="0" y="0" width="130" height="30" printWhenGroupChanges="DateGroup">
                        <printWhenExpression><![CDATA[$V{DateGroup_COUNT} == 1]]></printWhenExpression>
                    </reportElement>
                    <textElement>
                        <paragraph lineSpacing="Fixed"/>
                    </textElement>
                    <textFieldExpression><![CDATA[$F{Date}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="134" y="0" width="116" height="30"/>
                    <textFieldExpression><![CDATA[$F{Name}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="260" y="0" width="100" height="30"/>
                    <textElement textAlignment="Right"/>
                    <textFieldExpression><![CDATA[$V{groupTotal}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="458" y="0" width="100" height="30"/>
                    <textElement textAlignment="Right"/>
                    <textFieldExpression><![CDATA[$V{DateGroup_COUNT}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="389" y="0" width="68" height="30"/>
                    <textFieldExpression><![CDATA[$F{Group}]]></textFieldExpression>
                </textField>
            </band>
        </groupFooter>
    </group>
    <columnHeader>
        <band height="30" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="0" width="130" height="30"/>
                <text><![CDATA[Date]]></text>
            </staticText>
            <staticText>
                <reportElement x="134" y="0" width="116" height="30"/>
                <text><![CDATA[Name]]></text>
            </staticText>
            <staticText>
                <reportElement x="260" y="0" width="100" height="30"/>
                <textElement textAlignment="Right"/>
                <text><![CDATA[Amount]]></text>
            </staticText>
            <staticText>
                <reportElement x="458" y="0" width="100" height="30"/>
                <textElement textAlignment="Right"/>
                <text><![CDATA[DateGroup_COUNT]]></text>
            </staticText>
            <staticText>
                <reportElement x="390" y="0" width="68" height="30"/>
                <text><![CDATA[Group]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="40" splitType="Stretch">
            <printWhenExpression><![CDATA[!EQUALS($F{Group}, "Y")]]></printWhenExpression>
            <textField evaluationTime="Group" evaluationGroup="DateGroup" pattern="yyyy-MM-dd">
                <reportElement isPrintRepeatedValues="false" x="0" y="0" width="130" height="30" printWhenGroupChanges="DateGroup">
                    <printWhenExpression><![CDATA[$V{DateGroup_COUNT} == 1]]></printWhenExpression>
                </reportElement>
                <textFieldExpression><![CDATA[$F{Date}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="134" y="0" width="116" height="30"/>
                <textFieldExpression><![CDATA[$F{Name}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="260" y="0" width="100" height="30"/>
                <textElement textAlignment="Right"/>
                <textFieldExpression><![CDATA[$F{Amount}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="458" y="0" width="100" height="30"/>
                <textElement textAlignment="Right"/>
                <textFieldExpression><![CDATA[$V{DateGroup_COUNT}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="389" y="0" width="68" height="30"/>
                <textFieldExpression><![CDATA[$F{Group}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

如果我对任务的理解正确,您只需创建两个组并显示第二组的总计。

您可以通过 5 个步骤完成此任务:

  1. DateName 字段对数据进行排序。
  2. Date 字段创建组
  3. Name 字段创建组
  4. 创建变量以显示每个组中所有 Amount 值的总和 Name
  5. 仅使用 详细信息 列 Header 条带创建简单报告。

报告模板

该模板正在使用基于您的 csv 文件的 数据适配器

jrxml代码:

<?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="duplicatetest" pageWidth="390" pageHeight="842" columnWidth="350" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="datasuppressiontestdata"/>
    <field name="Date" class="java.util.Date"/>
    <field name="Name" class="java.lang.String"/>
    <field name="Group" class="java.lang.String"/>
    <field name="Amount" class="java.math.BigDecimal"/>
    <sortField name="Date"/>
    <sortField name="Name"/>
    <variable name="groupTotal" class="java.math.BigDecimal" resetType="Group" resetGroup="nameGroup" calculation="Sum">
        <variableExpression><![CDATA[$F{Amount}]]></variableExpression>
    </variable>
    <group name="dateGroup">
        <groupExpression><![CDATA[$F{Date}]]></groupExpression>
    </group>
    <group name="nameGroup">
        <groupExpression><![CDATA[$F{Name}]]></groupExpression>
    </group>
    <columnHeader>
        <band height="30" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="0" width="130" height="30"/>
                <text><![CDATA[Date]]></text>
            </staticText>
            <staticText>
                <reportElement x="130" y="0" width="120" height="30"/>
                <text><![CDATA[Name]]></text>
            </staticText>
            <staticText>
                <reportElement x="250" y="0" width="100" height="30"/>
                <textElement textAlignment="Right"/>
                <text><![CDATA[Amount]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="30" splitType="Stretch">
            <textField pattern="yyyy-MM-dd">
                <reportElement x="0" y="0" width="130" height="30">
                    <printWhenExpression><![CDATA[$V{dateGroup_COUNT} == 1]]></printWhenExpression>
                </reportElement>
                <textFieldExpression><![CDATA[$F{Date}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="130" y="0" width="120" height="30">
                    <printWhenExpression><![CDATA[$V{nameGroup_COUNT} == 1]]></printWhenExpression>
                </reportElement>
                <textFieldExpression><![CDATA[$F{Name}]]></textFieldExpression>
            </textField>
            <textField evaluationTime="Auto">
                <reportElement x="250" y="0" width="100" height="30">
                    <printWhenExpression><![CDATA[$V{nameGroup_COUNT} == 1]]></printWhenExpression>
                </reportElement>
                <textFieldExpression><![CDATA[$V{groupTotal}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

我使用 printWhenExpression 属性 只显示每个 DateName 组的第一条记录的数据。我在这个技巧的帮助下删除了所有“重复项”。

isRemoveLineWhenBlank="true" 属性 的帮助下,我隐藏了所有空行(对于案例 $V{nameGroup_COUNT} > 1)。

最后一个技巧是为 textField 设置 evaluationTime="Auto" 属性 总和。

输出结果

生成的报告在 JSS:

中看起来像这样

我创建了新变量来读取 DateGroup 计数。每次更改日期时重置为 1。 并且仅在 Dategroup 为 1 时才打印 Date。

检查下面的 jrxml。它提供了您想要的输出。

<?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="duplicatetest" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="04479c0d-d012-45d8-a8d1-f281276aba62">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="datasuppressiontestdata"/>
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <field name="Date" class="java.util.Date"/>
    <field name="Name" class="java.lang.String"/>
    <field name="Group" class="java.lang.String"/>
    <field name="Amount" class="java.math.BigDecimal"/>
    <variable name="groupTotal" class="java.math.BigDecimal" resetType="Group" resetGroup="Aggregate" calculation="Sum">
        <variableExpression><![CDATA[$F{Amount}]]></variableExpression>
    </variable>
    <variable name="COUNT_GROUP_1" class="java.lang.Integer" resetType="Group" resetGroup="DateGroup" incrementType="Group" incrementGroup="DateGroup">
        <variableExpression><![CDATA[( $V{Aggregate_COUNT} == 1) ? $V{COUNT_GROUP_1} + 1 : $V{COUNT_GROUP_1}]]></variableExpression>
        <initialValueExpression><![CDATA[0]]></initialValueExpression>
    </variable>
    <group name="DateGroup">
        <groupExpression><![CDATA[$F{Date}]]></groupExpression>
    </group>
    <group name="Aggregate">
        <groupExpression><![CDATA[$F{Group}]]></groupExpression>
        <groupFooter>
            <band height="30">
                <printWhenExpression><![CDATA[EQUALS($F{Group}, "Y")]]></printWhenExpression>
                <textField pattern="yyyy-MM-dd">
                    <reportElement isPrintRepeatedValues="false" x="0" y="0" width="130" height="30" printWhenGroupChanges="DateGroup" uuid="16819840-1496-42ac-aec8-02e50a104608">
                        <printWhenExpression><![CDATA[$V{COUNT_GROUP_1}==1]]></printWhenExpression>
                    </reportElement>
                    <textElement>
                        <paragraph lineSpacing="Fixed"/>
                    </textElement>
                    <textFieldExpression><![CDATA[$F{Date}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="134" y="0" width="116" height="30" uuid="5008a25a-6654-4ffc-95dd-cdefac6b1c3a"/>
                    <textFieldExpression><![CDATA[$F{Name}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="260" y="0" width="100" height="30" uuid="c89330be-7a7b-4721-b881-10e5df8ced68"/>
                    <textElement textAlignment="Right"/>
                    <textFieldExpression><![CDATA[$V{groupTotal}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="458" y="0" width="100" height="30" uuid="5dd2909c-4ffb-4296-9a42-3656bed378fe"/>
                    <textElement textAlignment="Right"/>
                    <textFieldExpression><![CDATA[$V{DateGroup_COUNT}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="389" y="0" width="68" height="30" uuid="61b8eb92-90ec-4727-a69c-642f063f08fe"/>
                    <textFieldExpression><![CDATA[$F{Group}]]></textFieldExpression>
                </textField>
            </band>
        </groupFooter>
    </group>
    <columnHeader>
        <band height="30" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="0" width="130" height="30" uuid="f05195da-f90c-4026-9e38-89a902853871"/>
                <text><![CDATA[Date]]></text>
            </staticText>
            <staticText>
                <reportElement x="134" y="0" width="116" height="30" uuid="2d40f4cc-a795-4e67-9fac-7a6e77085de3"/>
                <text><![CDATA[Name]]></text>
            </staticText>
            <staticText>
                <reportElement x="260" y="0" width="100" height="30" uuid="e5c44c50-93da-45a0-8a21-18fab3295972"/>
                <textElement textAlignment="Right"/>
                <text><![CDATA[Amount]]></text>
            </staticText>
            <staticText>
                <reportElement x="458" y="0" width="100" height="30" uuid="1b22596b-22e0-4b22-b2ab-9ed699ddd61f"/>
                <textElement textAlignment="Right"/>
                <text><![CDATA[DateGroup_COUNT]]></text>
            </staticText>
            <staticText>
                <reportElement x="390" y="0" width="68" height="30" uuid="8210e103-4473-4a5e-a915-a568356f76ac"/>
                <text><![CDATA[Group]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="40" splitType="Stretch">
            <printWhenExpression><![CDATA[!EQUALS($F{Group}, "Y")]]></printWhenExpression>
            <textField pattern="yyyy-MM-dd">
                <reportElement isPrintRepeatedValues="false" x="0" y="0" width="130" height="30" printWhenGroupChanges="DateGroup" uuid="9112a549-c2e1-4b49-996e-d12f5f1307cf">
                    <printWhenExpression><![CDATA[$V{COUNT_GROUP_1}==1]]></printWhenExpression>
                </reportElement>
                <textFieldExpression><![CDATA[$F{Date}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="134" y="0" width="116" height="30" uuid="111ee635-3426-4a2b-8f68-3e4e6dc003c3"/>
                <textFieldExpression><![CDATA[$F{Name}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="260" y="0" width="100" height="30" uuid="c0eac89a-54ac-4480-9fa1-3c841aed7d89"/>
                <textElement textAlignment="Right"/>
                <textFieldExpression><![CDATA[$F{Amount}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="458" y="0" width="100" height="30" uuid="b2ff7aaf-ab81-4b3d-a45f-9094044af7bf"/>
                <textElement textAlignment="Right"/>
                <textFieldExpression><![CDATA[$V{DateGroup_COUNT}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="389" y="0" width="68" height="30" uuid="5ac47f28-41e3-4940-bcdb-91b2ba85f30b"/>
                <textFieldExpression><![CDATA[$F{Group}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>