JasperReport打印重复值,即使设置为不重复

时间:2014-03-24 10:12:32

标签: sql jasper-reports

我正在组合一个sql列并使用组计数只为组合项打印出一行。但是我得到了这个结果:

enter image description here

这是我的代码:(对不起,如果它很乱)

<?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="TableReceipt" pageWidth="225" pageHeight="800" whenNoDataType="AllSectionsNoDetail" columnWidth="195" leftMargin="5" rightMargin="25" topMargin="20" bottomMargin="20" isFloatColumnFooter="true" isIgnorePagination="true" uuid="516cb110-18eb-4a7a-865b-66c019f5be1a">
    <property name="ireport.scriptlethandling" value="0"/>
    <property name="ireport.encoding" value="UTF-8"/>
    <property name="ireport.zoom" value="1.9965000000000035"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <import value="net.sf.jasperreports.engine.*"/>
    <import value="java.util.*"/>
    <import value="net.sf.jasperreports.engine.data.*"/>
    <parameter name="ticketId" class="java.lang.Integer">
        <defaultValueExpression><![CDATA[2]]></defaultValueExpression>
    </parameter>
    <parameter name="isSale" class="java.lang.Boolean">
        <defaultValueExpression><![CDATA[false]]></defaultValueExpression>
    </parameter>
    <parameter name="endTicket" class="java.lang.Boolean">
        <defaultValueExpression><![CDATA[false]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[SELECT
    t.TICKET_ITEM_ID,
    TICKET_ITEM_DESC,
    TICKET_ITEM_TOTAL,
    ticket.TICKET_ID,
    TICKET_CUSTOMER_NAME,
    TICKET_CHECK_TOTAL,
    TICKET_TOTAL_TAX,
    TICKET_ITEM_NUMBER
FROM
    ticket_item AS t,
    ticket
WHERE
    t.TICKET_ID = $P{ticketId}
AND
    ticket.TICKET_ID = $P{ticketId}
AND
    TICKET_ITEM_VOID = 0]]>
    </queryString>
    <field name="TICKET_ITEM_ID" class="java.lang.Integer">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="TICKET_ITEM_DESC" class="java.lang.String">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="TICKET_ITEM_TOTAL" class="java.lang.Double">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="TICKET_ID" class="java.lang.Integer">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="TICKET_CUSTOMER_NAME" class="java.lang.String">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="TICKET_CHECK_TOTAL" class="java.lang.Double">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="TICKET_TOTAL_TAX" class="java.lang.Double">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="TICKET_ITEM_NUMBER" class="java.lang.Integer">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <variable name="15per" class="java.lang.Double">
        <variableExpression><![CDATA[($F{TICKET_CHECK_TOTAL}-$F{TICKET_TOTAL_TAX})*0.15]]></variableExpression>
    </variable>
    <variable name="18per" class="java.lang.Double">
        <variableExpression><![CDATA[($F{TICKET_CHECK_TOTAL}-$F{TICKET_TOTAL_TAX})*0.18]]></variableExpression>
    </variable>
    <variable name="20per" class="java.lang.Double">
        <variableExpression><![CDATA[($F{TICKET_CHECK_TOTAL}-$F{TICKET_TOTAL_TAX})*0.20]]></variableExpression>
    </variable>
    <variable name="count" class="java.lang.Integer" resetType="None">
        <variableExpression><![CDATA[$V{item_name_COUNT}]]></variableExpression>
        <initialValueExpression><![CDATA[$V{item_name_COUNT}]]></initialValueExpression>
    </variable>
    <group name="item_name">
        <groupExpression><![CDATA[$F{TICKET_ITEM_NUMBER}]]></groupExpression>
    </group>
    <title>
        <band height="20" splitType="Stretch">
            <subreport>
                <reportElement uuid="f2cebdf8-f1d6-413f-9c03-914be928988c" x="0" y="20" width="195" height="0"/>
                <subreportParameter name="top">
                    <subreportParameterExpression><![CDATA[1]]></subreportParameterExpression>
                </subreportParameter>
                <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                <subreportExpression><![CDATA["subreports/ticket_lines.jasper"]]></subreportExpression>
            </subreport>
            <staticText>
                <reportElement uuid="ffbd8a47-40c3-4210-8301-913b3ef152e6" x="0" y="0" width="195" height="20">
                    <printWhenExpression><![CDATA[!$P{isSale}]]></printWhenExpression>
                </reportElement>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="Calibri" size="12" isBold="true"/>
                </textElement>
                <text><![CDATA[NOT A SALE TICKET]]></text>
            </staticText>
        </band>
    </title>
    <pageHeader>
        <band height="36">
            <subreport>
                <reportElement uuid="a1231c58-cd4f-46d7-87ce-a277abb52bc6" x="0" y="0" width="195" height="36">
                    <printWhenExpression><![CDATA[$F{TICKET_CUSTOMER_NAME} == null]]></printWhenExpression>
                </reportElement>
                <subreportParameter name="ticketId">
                    <subreportParameterExpression><![CDATA[$P{ticketId}]]></subreportParameterExpression>
                </subreportParameter>
                <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                <subreportExpression><![CDATA["subreports/details.jasper"]]></subreportExpression>
            </subreport>
            <subreport>
                <reportElement uuid="f47b2daf-b879-4fd6-9168-f1893a5a323f" x="0" y="0" width="195" height="36">
                    <printWhenExpression><![CDATA[$F{TICKET_CUSTOMER_NAME} != null]]></printWhenExpression>
                </reportElement>
                <subreportParameter name="ticketId">
                    <subreportParameterExpression><![CDATA[$P{ticketId}]]></subreportParameterExpression>
                </subreportParameter>
                <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                <subreportExpression><![CDATA["subreports/customerDetails.jasper"]]></subreportExpression>
            </subreport>
        </band>
    </pageHeader>
    <columnHeader>
        <band height="11" splitType="Stretch"/>
    </columnHeader>
    <detail>
        <band height="11" splitType="Stretch">
            <textField isStretchWithOverflow="true" isBlankWhenNull="true">
                <reportElement uuid="5b3ae499-3805-43ed-8f73-5a07bb67d2be" isPrintRepeatedValues="false" x="20" y="0" width="140" height="11" isRemoveLineWhenBlank="true" isPrintWhenDetailOverflows="true" printWhenGroupChanges="item_name"/>
                <textElement verticalAlignment="Middle">
                    <font fontName="Calibri" size="9" isBold="false"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{TICKET_ITEM_DESC}]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" pattern="#,##0.00" isBlankWhenNull="true">
                <reportElement uuid="16aac7b5-9688-4a78-860a-073cfca5fb9e" isPrintRepeatedValues="false" x="160" y="0" width="35" height="11" isRemoveLineWhenBlank="true" isPrintWhenDetailOverflows="true" printWhenGroupChanges="item_name"/>
                <textElement textAlignment="Right" verticalAlignment="Middle">
                    <font fontName="Calibri" size="9" isBold="false"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{TICKET_ITEM_TOTAL}]]></textFieldExpression>
            </textField>
            <subreport>
                <reportElement uuid="95f2a214-786a-45f7-ad12-2fc2befb5351" positionType="Float" isPrintRepeatedValues="false" x="0" y="11" width="195" height="0" isRemoveLineWhenBlank="true"/>
                <subreportParameter name="TICKET_ITEM_ID">
                    <subreportParameterExpression><![CDATA[$F{TICKET_ITEM_ID}]]></subreportParameterExpression>
                </subreportParameter>
                <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                <subreportExpression><![CDATA["subreports/modifiers.jasper"]]></subreportExpression>
            </subreport>
            <textField isStretchWithOverflow="true" evaluationTime="Group" evaluationGroup="item_name" isBlankWhenNull="true">
                <reportElement uuid="0536d26c-7ce5-4cb6-a3ea-7a8f3303ac25" isPrintRepeatedValues="false" mode="Transparent" x="0" y="0" width="20" height="11" isRemoveLineWhenBlank="true" isPrintWhenDetailOverflows="true" printWhenGroupChanges="item_name"/>
                <textElement verticalAlignment="Middle">
                    <font fontName="Calibri" size="9"/>
                </textElement>
                <textFieldExpression><![CDATA[$V{item_name_COUNT}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <summary>
        <band height="160">
            <subreport runToBottom="false">
                <reportElement uuid="7fc03f4a-fc6e-486f-8194-c7e6f770f5ff" positionType="Float" x="0" y="59" width="195" height="0" isPrintWhenDetailOverflows="true"/>
                <subreportParameter name="top">
                    <subreportParameterExpression><![CDATA[0]]></subreportParameterExpression>
                </subreportParameter>
                <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                <subreportExpression><![CDATA["subreports/ticket_lines.jasper"]]></subreportExpression>
            </subreport>
            <subreport runToBottom="false">
                <reportElement uuid="3dffcb5c-fda5-483e-a8e6-d584d2343dc6" x="0" y="0" width="195" height="0" isPrintWhenDetailOverflows="true"/>
                <subreportParameter name="ticketId">
                    <subreportParameterExpression><![CDATA[$P{ticketId}]]></subreportParameterExpression>
                </subreportParameter>
                <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                <subreportExpression><![CDATA["subreports/totals.jasper"]]></subreportExpression>
            </subreport>
            <subreport>
                <reportElement uuid="64e108d9-e944-402d-b5a4-83c618f64521" positionType="Float" x="0" y="68" width="195" height="92" isRemoveLineWhenBlank="true"/>
                <subreportParameter name="ticketId">
                    <subreportParameterExpression><![CDATA[$P{ticketId}]]></subreportParameterExpression>
                </subreportParameter>
                <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                <subreportExpression><![CDATA["subreports/deliveryDetails.jasper"]]></subreportExpression>
            </subreport>
            <subreport runToBottom="false">
                <reportElement uuid="6757917e-b848-4d29-ad8b-7df6ef333561" positionType="Float" x="0" y="15" width="195" height="0" isPrintWhenDetailOverflows="true">
                    <printWhenExpression><![CDATA[$P{endTicket}]]></printWhenExpression>
                </reportElement>
                <subreportParameter name="ticketId">
                    <subreportParameterExpression><![CDATA[$P{ticketId}]]></subreportParameterExpression>
                </subreportParameter>
                <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                <subreportExpression><![CDATA["subreports/condensed_transactions.jasper"]]></subreportExpression>
            </subreport>
            <textField pattern="¤ #,##0.00">
                <reportElement uuid="f309bce5-1d1b-475f-b6e0-91d254b20ae4" positionType="Float" x="95" y="15" width="55" height="13" isRemoveLineWhenBlank="true">
                    <printWhenExpression><![CDATA[!$P{endTicket}]]></printWhenExpression>
                </reportElement>
                <textElement verticalAlignment="Middle">
                    <font fontName="Calibri"/>
                </textElement>
                <textFieldExpression><![CDATA[$V{15per}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement uuid="67bd8b7a-122e-435f-abf5-e7a109c4f7b8" positionType="Float" x="61" y="41" width="34" height="13" isRemoveLineWhenBlank="true">
                    <printWhenExpression><![CDATA[!$P{endTicket}]]></printWhenExpression>
                </reportElement>
                <textElement verticalAlignment="Middle">
                    <font fontName="Calibri"/>
                </textElement>
                <text><![CDATA[20% -]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="69ee0f5f-378d-4fa0-b3f5-bd1dc337d71c" positionType="Float" x="61" y="28" width="34" height="13" isRemoveLineWhenBlank="true">
                    <printWhenExpression><![CDATA[!$P{endTicket}]]></printWhenExpression>
                </reportElement>
                <textElement verticalAlignment="Middle">
                    <font fontName="Calibri"/>
                </textElement>
                <text><![CDATA[18% -]]></text>
            </staticText>
            <textField pattern="¤ #,##0.00">
                <reportElement uuid="1747abc9-812c-4157-a717-fc6774fb92d7" positionType="Float" x="95" y="28" width="55" height="13" isRemoveLineWhenBlank="true">
                    <printWhenExpression><![CDATA[!$P{endTicket}]]></printWhenExpression>
                </reportElement>
                <textElement verticalAlignment="Middle">
                    <font fontName="Calibri"/>
                </textElement>
                <textFieldExpression><![CDATA[$V{18per}]]></textFieldExpression>
            </textField>
            <textField pattern="¤ #,##0.00">
                <reportElement uuid="ec57b3a8-4b29-40e5-b05c-1b90cab7558f" positionType="Float" x="95" y="41" width="55" height="13" isRemoveLineWhenBlank="true">
                    <printWhenExpression><![CDATA[!$P{endTicket}]]></printWhenExpression>
                </reportElement>
                <textElement verticalAlignment="Middle">
                    <font fontName="Calibri"/>
                </textElement>
                <textFieldExpression><![CDATA[$V{20per}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement uuid="8130693f-ca43-4a22-9b05-6f89707dc71f" positionType="Float" x="61" y="15" width="34" height="13" isRemoveLineWhenBlank="true">
                    <printWhenExpression><![CDATA[!$P{endTicket}]]></printWhenExpression>
                </reportElement>
                <textElement verticalAlignment="Middle">
                    <font fontName="Calibri"/>
                </textElement>
                <text><![CDATA[15% -]]></text>
            </staticText>
        </band>
    </summary>
</jasperReport>

我们正在查看的是$ V {item_name_COUNT}字段。即使Print Repeated Values设置为false,我仍然会得到它们。评估时间设置为分组,因为如果将其设置为其他任何内容,则计数将增加,并且不会为我提供正确的组计数。知道如何解决这个问题吗?我需要点数。

1 个答案:

答案 0 :(得分:0)

似乎简单的GROUP BY和COUNT解决了这个问题。

这是我使用的SQL:

SELECT
    t.TICKET_ITEM_ID,
    TICKET_ITEM_DESC,
    TICKET_ITEM_TOTAL,
    ticket.TICKET_ID,
    TICKET_CUSTOMER_NAME,
    TICKET_CHECK_TOTAL,
    TICKET_TOTAL_TAX,
    TICKET_ITEM_NUMBER,
    COUNT(TICKET_ITEM_NUMBER) AS COUNT
FROM
    ticket_item AS t,
    ticket
WHERE
    t.TICKET_ID = $P{ticketId}
AND
    ticket.TICKET_ID = $P{ticketId}
AND
    TICKET_ITEM_VOID = 0
GROUP BY
    TICKET_ITEM_NUMBER

其他一切都保持不变。