【问题标题】:Suppressing duplicate values in groups抑制组中的重复值
【发布时间】:2020-06-02 00:34:17
【问题描述】:

我有一份包含 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>

也不起作用,如下图所示。我已经包含了 Group 和 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>

【问题讨论】:

  • 我不认为建议的链接答案直接适用。第一个答案是一个很好的解决不同问题的方法,并提示我添加一个 DateGroup,但这仍然不能正确处理聚合带中的 Date 字段的显示。在这个阶段,我看不出第二个链接的答案是相关的。

标签: jasper-reports


【解决方案1】:

我创建了新变量来读取 DateGroup count 。在每个日期更改时重置为 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>

【讨论】:

    【解决方案2】:

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

    您可以通过 5 个步骤解决此任务:

    1. DateName 字段对数据进行排序。
    2. Date 字段创建组
    3. Name 字段创建组
    4. 创建变量以按Name 显示每个组中所有Amount 值的总和
    5. 仅使用 DetailColumn 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} &gt; 1 的情况)。

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

    输出结果

    生成的报告在 JSS 中如下所示:

    【讨论】:

    • 感谢您查看此内容。我提供的示例使用两个频段来演示选择性打开/关闭频段的问题。实际的报告有三个波段,根据不同的标准打开/关闭,每个列的值都有不同的派生,每个都有不同的字段。试图在一个频段中完成所有操作将需要有选择地打开/关闭字段,并且它们需要重叠。不易维护
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-23
    • 2021-03-04
    • 1970-01-01
    • 2020-05-08
    相关资源
    最近更新 更多