Jump to content
We've recently updated our Privacy Statement, available here ×

Using Sum variable in calculation, sum is wrong


kgentile1010

Recommended Posts

Hi,

 

I'm writing a report that has the % Total for each row of a group. Since we don't know the total before we finish

filling the group, I'm using a sub-report. My percent total is count/totalcount. A posting in the forum said I could

pass the count into the subreport as a parameter and calculate $P{count}/$V{TOTALCOUNT}.

 

Below is my data

 

NAME STATUS SCORE COUNT

---------------- ------------ ------------- -----

Safety Hired Average 2

Safety Hired Above Average 12

Safety Pre-Screened Average 345

Safety Pre-Screened Above Average 679

Safety Pre-Screened Below Average 19

 

 

My problem is the $V{TOTALCOUNT} is not counting the last row in my data when I use it in a $V{PERCENTTOTAL} calculation.

I've added some parameter defaults to simulate the data I pass to the report.

 

The defaults and filters

 

-- $P{name} set to Safety

-- $P{count} set to new Integer(345)

 

I have some PrintWhen expressions

 

In the Filter expression for the report query I filter on status

 

new Boolean(($F{status}.equals(new String("Pre-Screened"))))

 

For each Text Field Expression I filter -- new Boolean($F{name}.equals($P{name}))

 

If I run the report and return in the name group band

 

$F{count} $P{count} $V{TOTALCOUNT} $V{PERCENTTOTAL}

 

I get the following

 

19 345 1043 33.69%

 

 

I should not get 33.69%, I should be getting 345/1043 as a percent, which is 33.07%

 

In all my data groups, I can see that the totalcount is calculated correctly, but the last value in my row doesn't get

used to calculate the percent.

 

I've attached the report, can you see what's wrong?

 

Thank you so much for your help. I truly appreciate it. My report jrxml is below.

 

<?xml version="1.0" encoding="UTF-8" ?>

<!-- Created with iReport - A designer for JasperReports -->

<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">

<jasperReport

name="PercentTest2"

columnCount="1"

printOrder="Vertical"

orientation="Portrait"

pageWidth="396"

pageHeight="708"

columnWidth="396"

columnSpacing="0"

leftMargin="0"

rightMargin="0"

topMargin="0"

bottomMargin="0"

whenNoDataType="NoPages"

isTitleNewPage="false"

isSummaryNewPage="false"

isIgnorePagination="true">

<property name="ireport.scriptlethandling" value="0" />

<property name="ireport.encoding" value="UTF-8" />

<import value="java.util.*" />

<import value="net.sf.jasperreports.engine.*" />

<import value="net.sf.jasperreports.engine.data.*" />

 

<parameter name="name" isForPrompting="false" class="java.lang.String">

<defaultValueExpression ><![CDATA["Safety"]]></defaultValueExpression>

</parameter>

<parameter name="score" isForPrompting="false" class="java.lang.String">

<defaultValueExpression ><![CDATA[""]]></defaultValueExpression>

</parameter>

<parameter name="count" isForPrompting="false" class="java.lang.Integer">

<defaultValueExpression ><![CDATA[new Integer(345)]]></defaultValueExpression>

</parameter>

 

<field name="name" class="java.lang.String"/>

<field name="status" class="java.lang.String"/>

<field name="count" class="java.lang.Integer"/>

 

<variable name="PERCENTTOTAL" class="java.lang.Float" resetType="Report" calculation="Nothing">

<variableExpression><![CDATA[new Float($P{count}.floatValue()/$V{TOTALCOUNT}.floatValue())]]></variableExpression>

</variable>

<variable name="TOTALCOUNT" class="java.lang.Integer" resetType="Group" resetGroup="NAME" calculation="Sum">

<variableExpression><![CDATA[$F{count}]]></variableExpression>

</variable>

<filterExpression><![CDATA[new Boolean(($F{status}.equals(new String("Pre-Screened"))))]]></filterExpression>

 

<group name="NAME" >

<groupExpression><![CDATA[$F{name}]]></groupExpression>

<groupHeader>

<band height="0" isSplitAllowed="true" >

</band>

</groupHeader>

<groupFooter>

<band height="20" isSplitAllowed="true" >

<textField isStretchWithOverflow="false" pattern="#,##0.00 %" isBlankWhenNull="false" evaluationTime="Group" evaluationGroup="SCORE" hyperlinkType="None" hyperlinkTarget="Self" >

<reportElement

x="320"

y="0"

width="50"

height="20"

key="textField"

positionType="Float"

isRemoveLineWhenBlank="true">

<printWhenExpression><![CDATA[new Boolean($F{name}.equals($P{name}))]]></printWhenExpression>

</reportElement>

<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>

<textElement>

<font size="11"/>

</textElement>

<textFieldExpression class="java.lang.Float"><![CDATA[$V{PERCENTTOTAL}]]></textFieldExpression>

</textField>

<textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >

<reportElement

x="0"

y="0"

width="60"

height="20"

key="textField">

<printWhenExpression><![CDATA[new Boolean($F{name}.equals($P{name}))]]></printWhenExpression>

</reportElement>

<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>

<textElement>

<font/>

</textElement>

<textFieldExpression class="java.lang.Integer"><![CDATA[$F{count}]]></textFieldExpression>

</textField>

<textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >

<reportElement

x="60"

y="0"

width="80"

height="20"

key="textField">

<printWhenExpression><![CDATA[new Boolean($F{name}.equals($P{name}))]]></printWhenExpression>

</reportElement>

<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>

<textElement>

<font/>

</textElement>

<textFieldExpression class="java.lang.Integer"><![CDATA[$P{count}]]></textFieldExpression>

</textField>

<textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >

<reportElement

x="170"

y="0"

width="140"

height="20"

key="textField">

<printWhenExpression><![CDATA[new Boolean($F{name}.equals($P{name}))]]></printWhenExpression>

</reportElement>

<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>

<textElement>

<font/>

</textElement>

<textFieldExpression class="java.lang.Integer"><![CDATA[$V{TOTALCOUNT}]]></textFieldExpression>

</textField>

</band>

</groupFooter>

</group>

<group name="SCORE" >

<groupExpression><![CDATA[$P{score}]]></groupExpression>

<groupHeader>

<band height="0" isSplitAllowed="true" >

</band>

</groupHeader>

<groupFooter>

<band height="0" isSplitAllowed="true" >

</band>

</groupFooter>

</group>

<background>

<band height="2" isSplitAllowed="true" >

</band>

</background>

<title>

<band height="0" isSplitAllowed="true" >

</band>

</title>

<pageHeader>

<band height="0" isSplitAllowed="true" >

</band>

</pageHeader>

<columnHeader>

<band height="0" isSplitAllowed="true" >

</band>

</columnHeader>

<detail>

<band height="0" isSplitAllowed="true" >

</band>

</detail>

<columnFooter>

<band height="0" isSplitAllowed="true" >

</band>

</columnFooter>

<pageFooter>

<band height="0" isSplitAllowed="true" >

</band>

</pageFooter>

<summary>

<band height="0" isSplitAllowed="true" >

</band>

</summary>

</jasperReport>

Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...